Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7028325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:22:20+00:00 2026-05-28T00:22:20+00:00

This works: <?php //compare1.php //**CLASS AND OBJECT class Entry { private $s_ids; public function

  • 0

This works:

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

?>

With this sample file:

Start
S_id:      0611147
S_id:      0651134
End

Start
S_id:      0611125
S_id:      0651125
End

This does not work:

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

With this sample file:

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

The logic seems the same. I checked the plurals, and I checked the preg match functions. Everything in compare1.php seems to parallel compare2.php, but look at the differences in the outputs:

Output1:

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
                )

        )

Output 2:

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

        )

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

        )

)


)

How can the files be pretty much exactly the same, and yet the latter of the two returns a different kind of result? Shouldn’t [0] => 5% Grapefruit be [0] => Strong off; [1] => 5% Grapefruit? And [0] => Cafe Milk Tea be [0] => Coco; [1] => Cafe Milk Tea?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:22:21+00:00Added an answer on May 28, 2026 at 12:22 am

    Your isStart_entry function is looking for <title> instead of <list>. It should be coded like this:

    function isStart_entry($line)
    {
      return preg_match('/^<list>.*$/', $line);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is an array of user states stored in the session. This works: <?php
Im writing a php script to compare the similarity of 2 strings. This works
I've been doing some HTML scraping in PHP using regular expressions. This works, but
I can't figure out why... This works: <?php if($_POST['test']) echo posted; ?> <form method=POST
When I run this code: >> I = imread('D:\Works\matlab\SecCode.php.png','png'); >> imshow(I); It always shows
I wrote this function in php to check user/pass against account on linux server.
http://steph.net23.net/work.php here is my test link. This page has a jquery script in it
How can I make something like this below work? <?PHP $_SESSION['signup_errors']['test1']; $_SESSION['signup_errors']['test2']; $_SESSION['signup_errors']['test3']; $_SESSION['signup_errors']['test4'];
This works (prints, for example, 3 arguments): to run argv do shell script echo
This works, but is it the proper way to do it??? I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.