Search code examples
phpsyntaxlogicundefinedfgets

Parsing program: How am I loading my object incorrectly?


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.*$/'


Solution

  • 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.