Php 解析程序:如何错误地加载对象?

Php 解析程序:如何错误地加载对象?,php,syntax,logic,undefined,fgets,Php,Syntax,Logic,Undefined,Fgets,在我看来,if(isStart($line)){}和if(isEnd($line))块把事情放在了错误的范围内。问题区域的注释是“/***问题区域*/” 这是我的解析程序: <?php //**CLASS AND OBJECT */ class Entry { private $reason; private $s_id; public function __construct() { $this->reason =

在我看来,
if(isStart($line)){}
if(isEnd($line))
块把事情放在了错误的范围内。问题区域的注释是“
/***问题区域*/

这是我的解析程序:

<?php

  //**CLASS AND OBJECT */
  class Entry
  {
    private $reason;
    private $s_id;

    public function __construct()
    {
      $this->reason     = '';
      $this->s_id       = '';
    }

      //** GETTERS AND SETTERS */
    public function SetReason($reason)
    {
      $this->reason = $reason;
    }

    public function GetReason()
    {
      return $this->reason;
    }

    public function SetS_id($s_id)
    {
      $this->s_id = $s_id;
    }

    public function GetS_id()
    {
      return $this->s_id;
    }
  }

  //** EXTRACTION FUNCTION(S)
  function extractReason($line)
  {
    $matches;
    preg_match('/^Reason:\s+(.*)\s+$/', $line, $matches);
    return $matches[1];
  }  

  function extractS_id($line)
  {
    $matches;
    preg_match('/^S_id:\s+(.*)\s+$/', $line, $matches);
    return $matches[1];
  }

  //** LINE CONTAINST DESIRED EXTRACTION CHECK */
  function isStart($line)
  {
    return preg_match('/^Start$/', $line);
  }

  function isReason($line)
  {
    return preg_match('/^Reason:\s+(.*)$/', $line);
  }

  function isS_id($line)
  {
    return preg_match('/^S_id:\s+(.*)$/', $line);
  }

  function isContent($line)
  {
    return preg_match('/.*$/', $line);
  }

  function isEnd($line)
  {
    return preg_match('/^End$/', $line);
  }




  //** DEFINITION */
  $fName = 'obfile_extractsample.txt';
  $fh    = fopen($fName, 'r');
  $line;
  $entry;
  $entrys = array();

  //** PARSE OPERATION
  if ($fh === FALSE)
    die ('Failed to open file.');

  //**START PROBLEM AREA */
  while (($line = fGets($fh)) !== FALSE)
  {
    if (isStart($line)){
      $entry = new Entry();
      if (isReason($line)){
        $entry->SetReason(extractReason($line));
      }
      if (isS_id($line)){
        $entry->SetS_id(extractS_id($line));
      }
      if (isEnd($line)){
        $entrys[] = $entry;
      }
    }
  }
  //***END PROBLEM AREA */

  echo "<pre>";
    print_r($entrys);
  echo "</pre>";
  fclose($fh);

?>
这是输出:

    if (isContent($line)){
      $entry = new Entry();
      $entry->SetReason(extractReason($line));
      $entry->SetS_id(extractS_id($line));
      $entrys[] = $entry;
    }
重要编辑

isStart
isEnd
函数中的正则表达式字符串输入不正确。在这一行的结尾前有一些看不见的符号。正确的正则表达式模式是:
'/^Start.*$/'
'/^End.*$/'

此位:


将始终创建一个新的
条目
并将其添加到数组中,但它可以检测到的唯一字段是
原因:…
样式:…
。因此,大多数行的结果都是空的
条目
s.

是的,我只想要
原因:
s\u id:
,但是输出都是乱七八糟的。输出应如下所示:。结果太多,preg_match()模式错误,结果是2的数组。我应该输出
$match[1]
,但它会抛出大量错误:
未定义的偏移量1
@Wolfpack'08:正如我所说,您正在为每一行创建一个新的
条目。相反,您需要在进入循环之前创建一个
条目
,然后在每次检测到空行时创建一个新的
条目
。(但请注意,
isContent
中有一个bug:它将始终返回
1
。您需要先修复它,然后才能让其他内容正常工作。)ruakh,我应该将
$entry=new entry()
if块之外
?我希望
isContent
返回
1
的原因是我想检查每一行。不过,我明白你的意思。嗯,我注意到的一件事是,这条线必须以回车结束,否则它将无法找到匹配的结果。我担心的是有
$entrys[]
$entry=newentry()
if块一起输入
。如果其中一个或两个不是主键呢?在大多数情况下,只有一个主键存在。
Array()
    if (isContent($line)){
      $entry = new Entry();
      $entry->SetReason(extractReason($line));
      $entry->SetS_id(extractS_id($line));
      $entrys[] = $entry;
    }