类似的解析程序:为什么一个工作,而另一个不工作?

时间:2012-01-11 02:21:22

标签: php parsing logic fgets string-parsing

这有效:

<?php
  //compare1.php
  //**CLASS AND OBJECT
  class Entry
  {
    private $s_ids;

    public function __construct()
    {
      $this->s_ids      = array();
    }

    public function AddS_id($s_id)
    {
      $this->s_id[] = $s_id;
    }

    public function SetS_ids($s_ids)
    {
      $this->s_ids[] = $s_ids;
    }

    public function GetS_id($i)
    {
      if ($i > count($s_ids))
        throw new Exception('Out of bounds.');
      return $this->s_ids[$i];
    }

    public function GetS_ids()
    {
      return $this->s_ids;
    }
  }


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


  //LINE CHECKS
  function isStart($line)
  {
    return preg_match('/^Start.*$/', $line);
  }

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

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


  //VARIABLE DECLARATION
  $fName = 'sample1.txt';
  $fh    = fopen($fName, 'r');
  $line;
  $entry;
  $entrys = array();


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

  while (($line = fGets($fh)) !== FALSE)
  {
    if (isStart($line)){
      $entry = new Entry();
    }
    if (isS_id($line)){
      $entry->SetS_ids(extractS_ids($line));
    }
    if (isEnd($line)){
      $entrys[] = $entry;
    }
  }


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

?>

使用此示例文件:

Start
S_id:      0611147
S_id:      0651134
End

Start
S_id:      0611125
S_id:      0651125
End

这不起作用:

<?php
  //compare2.php
  //CLASS AND OBJECT
  class Entry
    {
      private $titles; 

      public function __construct()
      {
        $this->titles = array();
      }

      public function AddType($title)
      {
        $this->titles[] = $title;
      }

      public function SetTitles($titles)
      {
        $this->titles[] = $titles;
      }

      public function GetTitle($i)
      {
        if ($i > count($titles))
          throw new Exception('Out of bounds.');
        return $this->titles[$i];
      }

      public function GetTitles()
      {
        return $this->titles;
      }
    }

  //EXTRACTION FUNCTION
  function extractTitles($line)
  {
    $matches;
    preg_match('/^<title>(.*)<\/title>.*$/', $line, $matches);
    return $matches[1];
  }

  //LINE CHECK FUNCTION
  function isStart_entry($line)
  {
    return preg_match('/^<title>.*$/', $line);
  }

  function isTitle($line)
  {
    return preg_match('/^<title>.*<\/title>.*$/', $line);
  }

  function isClose_entry($line)
  {
    return preg_match('/^<\/list>.*$/', $line);
  }


  //DECLARATIONS
  $fName = 'sample2.txt';
  $fh    = fopen($fName, 'r');
  $line;
  $entry;
  $entrys = array();


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

  while (($line = fgets($fh)) !== FALSE)
  {
    if (isStart_entry($line)){
      $entry = new Entry();
    }
    if (isTitle($line)){
      $entry->SetTitles(extractTitles($line));
    }
    if (isClose_entry($line)){
      $entrys[] = $entry;
    }
  }

  // Dump the results.
  echo "<pre>";
    print_r($entrys);
  echo "</pre>";

  // Close the file.
  fclose($fh);
?>

使用此示例文件:

<list>
<title>Coco</title>
<title>Cafe Milk Tea</title>
</list>
<list>
<title>Strong Off</title>
<title>5% Grapefruit</title>
</list>

逻辑似乎是一样的。我检查了复数,我检查了preg match函数。 compare1.php中的所有内容似乎都与compare2.php平行,但请查看输出中的差异:

输出1:

Array
(
    [0] => Entry Object
        (
            [s_ids:Entry:private] => Array
                (
                    [0] => 0611147
                    [1] => 0651134
                )

        )

    [1] => Entry Object
        (
            [s_ids:Entry:private] => Array
                (
                    [0] => 0611125
                    [1] => 0651125
                )

        )

输出2:

Array
(
    [0] => Entry Object
        (
            [titles:Entry:private] => Array
                (
                    [0] => Cafe Milk Tea
                )

        )

    [1] => Entry Object
        (
            [titles:Entry:private] => Array
                (
                    [0] => 5% Grapefruit
                )

        )

)


)

文件如何几乎完全相同,但两者中的后者会返回不同类型的结果? [0] => 5% Grapefruit [0] => Strong off; [1] => 5% Grapefruit不应该[0] => Cafe Milk Tea吗? [0] => Coco; [1] => Cafe Milk Tea是{{1}}?

1 个答案:

答案 0 :(得分:1)

您的isStart_entry功能正在寻找<title>而不是<list>。它应该像这样编码:

function isStart_entry($line)
{
  return preg_match('/^<list>.*$/', $line);
}