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

发布于 2024-12-25 20:39:26 字数 2705 浏览 1 评论 0原文

在我看来,if (isStart($line)){}if (isEnd($line)) 块将事物放入了错误的范围。问题区域在“/***PROBLEM AREA */”周围进行注释。

这是我的解析程序:

<?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);

?>

这是我的示例文件:

Start
Name:      David Foster  
Out Time:  4:36 p.m.    
Back Time: 4:57 p.m.
Reason:    Lunch
S_id:      0611125
End

Start
Name:      Brenda Banks  
Out Time:  5:53 a.m.    
Back Time: 6:30 a.m.
Reason:    Personal
S_id:      0611147
End

这是输出:

Array()

重要编辑

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

In my opinion, the if (isStart($line)){} and if (isEnd($line)) blocks put things into the wrong scope. The problem area is commented around "/***PROBLEM AREA */".

This is my parsing program:

<?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);

?>

This is my example file:

Start
Name:      David Foster  
Out Time:  4:36 p.m.    
Back Time: 4:57 p.m.
Reason:    Lunch
S_id:      0611125
End

Start
Name:      Brenda Banks  
Out Time:  5:53 a.m.    
Back Time: 6:30 a.m.
Reason:    Personal
S_id:      0611147
End

This is the output:

Array()

IMPORTANT EDIT

The regex string input in the isStart and isEnd functions was incorrect. There were some invisible symbols before the end of the line. The correct regex patterns are: '/^Start.*$/' and '/^End.*$/'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我一向站在原地 2025-01-01 20:39:26

这个 bit:

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

总是会创建一个新的 Entry 并将其添加到数组中,但它唯一可以检测到的字段是 Reason: ...Style: 。 ..。因此,大多数行都会产生空的 Entry

This bit:

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

will always create a new Entry and add it to your array, but the only fields it can detect are Reason: ... and Style: .... So most of the lines result in empty Entrys.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文