fgets() and <<<EOT

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gert Kok

    fgets() and <<<EOT

    When fgets() reads a string that ands with <<<EOT, the string is
    truncated after << and a lot of following input is discarded.

    When <<<EOT is changed to <<< EOT, behaviour is as I expect it.

    Can it be explained why the content of a file is interfering with the
    behaviour of fgets()?
  • Jerry Stuckle

    #2
    Re: fgets() and &lt;&lt;&lt; EOT

    Gert Kok wrote:
    When fgets() reads a string that ands with <<<EOT, the string is
    truncated after << and a lot of following input is discarded.
    >
    When <<<EOT is changed to <<< EOT, behaviour is as I expect it.
    >
    Can it be explained why the content of a file is interfering with the
    behaviour of fgets()?
    >
    Sorry, my crystal ball is broken. I can't see your code or the data in
    the file you're trying to read.

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • The Hajj

      #3
      Re: fgets() and &lt;&lt;&lt; EOT

      On Aug 14, 10:26 am, Jerry Stuckle <jstuck...@attg lobal.netwrote:
      Gert Kok wrote:
      When fgets() reads a string that ands with <<<EOT, the string is
      truncated after << and a lot of following input is discarded.
      >
      When <<<EOT is changed to <<< EOT, behaviour is as I expect it.
      >
      Can it be explained why the content of a file is interfering with the
      behaviour of fgets()?
      >
      Sorry, my crystal ball is broken. I can't see your code or the data in
      the file you're trying to read.
      >
      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstuck...@attgl obal.net
      =============== ===
      I'll send one of the monkeys in to fix that.

      if you have >= 4.2.0 there is no need to specify a length.

      If you are just reading(whole) you could use file_get_conten ts();

      Comment

      • Gert Kok

        #4
        Re: fgets() and &lt;&lt;&lt; EOT

        This is the reading part:

        $fp = fopen($filename ,"r");
        $n = 0;
        while ($regel = fgets($fp)) {
        ++$n;
        if (strpos($regel, "//!")) {
        echo $filename,"\n",
        sprintf("%3d ",$n),substr(tr im($regel),4)," \n";
        do {
        $regel = fgets($fp);
        ++$n;
        echo "\t",trim($rege l),"\n";
        } while (!feof($fp) && !strpos($regel, ';'));
        echo "\n";
        }
        }
        fclose($fp);



        This was in the input file:

        //! Bevestigingsmai l: indien lunch geselecteerd.
        $bericht .= <<<EOT

        De lunch hebben wij al voor u gereserveerd.
        EOT;


        The 1st resulting $regel from the line with <<<EOT

        was

        $bericht .= <<



        Jerry Stuckle schreef:
        Gert Kok wrote:
        >When fgets() reads a string that ands with <<<EOT, the string is
        >truncated after << and a lot of following input is discarded.
        >>
        >When <<<EOT is changed to <<< EOT, behaviour is as I expect it.
        >>
        >Can it be explained why the content of a file is interfering with the
        >behaviour of fgets()?
        >>
        >
        Sorry, my crystal ball is broken. I can't see your code or the data in
        the file you're trying to read.
        >

        Comment

        • Jerry Stuckle

          #5
          Re: fgets() and &lt;&lt;&lt; EOT

          Gert Kok wrote:
          Jerry Stuckle schreef:
          >Gert Kok wrote:
          >>When fgets() reads a string that ands with <<<EOT, the string is
          >>truncated after << and a lot of following input is discarded.
          >>>
          >>When <<<EOT is changed to <<< EOT, behaviour is as I expect it.
          >>>
          >>Can it be explained why the content of a file is interfering with the
          >>behaviour of fgets()?
          >>>
          >>
          >Sorry, my crystal ball is broken. I can't see your code or the data
          >in the file you're trying to read.
          >>
          >
          This is the reading part:
          >
          $fp = fopen($filename ,"r");
          $n = 0;
          while ($regel = fgets($fp)) {
          ++$n;
          if (strpos($regel, "//!")) {
          echo $filename,"\n",
          sprintf("%3d ",$n),substr(tr im($regel),4)," \n";
          do {
          $regel = fgets($fp);
          ++$n;
          echo "\t",trim($rege l),"\n";
          } while (!feof($fp) && !strpos($regel, ';'));
          echo "\n";
          }
          }
          fclose($fp);
          >
          >
          >
          This was in the input file:
          >
          //! Bevestigingsmai l: indien lunch geselecteerd.
          $bericht .= <<<EOT
          >
          De lunch hebben wij al voor u gereserveerd.
          EOT;
          >
          >
          The 1st resulting $regel from the line with <<<EOT
          >
          was
          >
          $bericht .= <<
          >
          >
          >
          (Top posting fixed)

          Your problem is not the <<<EOT - it's in your comparison. When your
          '//!' starts in the first column, strpos returns 0. Your 'if' statement
          converts the 0 the a false and never executes the following code. A
          similar error occurs in your while() statement.

          I change those lines to the following and it worked as expected:

          if (strpos($regel, "//!") !== false) {

          and
          } while (!feof($fp) && strpos($regel,' ;') !== false);

          P.S. Please don't top post. Thanks.



          --
          =============== ===
          Remove the "x" from my email address
          Jerry Stuckle
          JDS Computer Training Corp.
          jstucklex@attgl obal.net
          =============== ===

          Comment

          • Gert Kok

            #6
            Re: fgets() and &lt;&lt;&lt; EOT

            Gert Kok wrote:
            >Jerry Stuckle schreef:
            >>Gert Kok wrote:
            >>>When fgets() reads a string that ands with <<<EOT, the string is
            >>>truncated after << and a lot of following input is discarded.
            >>>>
            >>>When <<<EOT is changed to <<< EOT, behaviour is as I expect it.
            >>>>
            >>>Can it be explained why the content of a file is interfering with
            >>>the behaviour of fgets()?
            >>>>
            >>>
            >>Sorry, my crystal ball is broken. I can't see your code or the data
            >>in the file you're trying to read.
            >>>
            >>
            This is the reading part:
            >
            $fp = fopen($filename ,"r");
            $n = 0;
            while ($regel = fgets($fp)) {
            ++$n;
            if (strpos($regel, "//!")) {
            echo $filename,"\n",
            sprintf("%3d ",$n),substr(tr im($regel),4)," \n";
            do {
            $regel = fgets($fp);
            ++$n;
            echo "\t",trim($rege l),"\n";
            } while (!feof($fp) && !strpos($regel, ';'));
            echo "\n";
            }
            }
            fclose($fp);
            >
            >
            >
            This was in the input file:
            >
            //! Bevestigingsmai l: indien lunch geselecteerd.
            $bericht .= <<<EOT
            >
            De lunch hebben wij al voor u gereserveerd.
            EOT;
            >
            >
            The 1st resulting $regel from the line with <<<EOT
            >
            was
            >
            $bericht .= <<
            >
            >
            >
            >
            (Top posting fixed)
            >
            Your problem is not the <<<EOT - it's in your comparison. When your
            '//!' starts in the first column, strpos returns 0. Your 'if' statement
            converts the 0 the a false and never executes the following code. A
            similar error occurs in your while() statement.
            >
            I change those lines to the following and it worked as expected:
            >
            if (strpos($regel, "//!") !== false) {
            >
            and
            } while (!feof($fp) && strpos($regel,' ;') !== false);
            >
            P.S. Please don't top post. Thanks.
            >
            >
            >
            The test on a boolean type is certainly the way to go.
            But it doesn't change the unexpected behaviour:

            --------------------------------------------------------------
            code (Corrected):
            --------------------------------------------------------------
            $fp = fopen($filename ,"r");
            $n = 0;
            while ($regel = fgets($fp)) {
            ++$n;
            if (strpos($regel, "//!") !== false) {
            echo $filename,"\n",
            sprintf("%3d ",$n),substr(tr im($regel),4)," \n";
            do {
            $regel = fgets($fp);
            ++$n;
            echo "\t",trim($rege l),"\n";
            if (strpos($regel, ';') !== false) {
            break;
            }
            } while (!feof($fp) );
            echo "\n";
            }
            }
            fclose($fp);

            --------------------------------------------------------------
            Inputfile:
            --------------------------------------------------------------
            <?php
            //! This is working fine (space)
            echo <<< EOT
            A
            B
            C
            EOT;
            //! Unexpected A
            echo <<<EOT
            D
            E
            F
            EOT;
            //! Unexpected B
            echo <<<EOT
            G
            H
            I
            EOT;
            //! Final (space)
            echo <<< EOT
            J
            K
            L
            EOT;
            ?>
            --------------------------------------------------------------
            Output for this file:
            --------------------------------------------------------------
            ../Untitled-1.php
            2 This is working fine (space)
            echo <<< EOT
            A
            B
            C
            EOT;

            ../Untitled-1.php
            8 Unexpected A
            echo <<<<<<< EOT
            J
            K
            L
            EOT;


            ---------------------------------------------------------------
            This is the PHP version:
            PHP Version 5.2.5

            System Windows NT SERVER 5.1 build 2600
            Build Date Nov 8 2007 23:18:08
            Configure Command cscript /nologo configure.js
            "--enable-snapshot-build" "--with-gd=shared"
            Server API Apache 2.0 Handler
            Virtual Directory Support enabled
            Configuration File (php.ini) Path C:\WINDOWS
            Loaded Configuration File C:\testserver\p hp.ini
            PHP API 20041225
            PHP Extension 20060613
            Zend Extension 220060519
            Debug Build no
            Thread Safety enabled
            Zend Memory Manager enabled
            IPv6 Support enabled
            Registered PHP Streams php, file, data, http, ftp, compress.zlib
            Registered Stream Socket Transports tcp, udp
            Registered Stream Filters convert.iconv.* , string.rot13,
            string.toupper, string.tolower, string.strip_ta gs, convert.*, consumed,
            zlib.*

            Jerry Stuckle schreef:

            Comment

            • Jerry Stuckle

              #7
              Re: fgets() and &lt;&lt;&lt; EOT

              Gert Kok wrote:
              >
              >Gert Kok wrote:
              >>Jerry Stuckle schreef:
              >>>Gert Kok wrote:
              >>>>When fgets() reads a string that ands with <<<EOT, the string is
              >>>>truncated after << and a lot of following input is discarded.
              >>>>>
              >>>>When <<<EOT is changed to <<< EOT, behaviour is as I expect it.
              >>>>>
              >>>>Can it be explained why the content of a file is interfering with
              >>>>the behaviour of fgets()?
              >>>>>
              >>>>
              >>>Sorry, my crystal ball is broken. I can't see your code or the data
              >>>in the file you're trying to read.
              >>>>
              >>>
              > This is the reading part:
              > >
              > $fp = fopen($filename ,"r");
              > $n = 0;
              > while ($regel = fgets($fp)) {
              > ++$n;
              > if (strpos($regel, "//!")) {
              > echo $filename,"\n",
              > sprintf("%3d ",$n),substr(tr im($regel),4)," \n";
              > do {
              > $regel = fgets($fp);
              > ++$n;
              > echo "\t",trim($rege l),"\n";
              > } while (!feof($fp) && !strpos($regel, ';'));
              > echo "\n";
              > }
              > }
              > fclose($fp);
              > >
              > >
              > >
              > This was in the input file:
              > >
              > //! Bevestigingsmai l: indien lunch geselecteerd.
              > $bericht .= <<<EOT
              > >
              > De lunch hebben wij al voor u gereserveerd.
              > EOT;
              > >
              > >
              > The 1st resulting $regel from the line with <<<EOT
              > >
              > was
              > >
              > $bericht .= <<
              > >
              > >
              > >
              >>
              >(Top posting fixed)
              >>
              >Your problem is not the <<<EOT - it's in your comparison. When your
              >'//!' starts in the first column, strpos returns 0. Your 'if'
              >statement converts the 0 the a false and never executes the following
              >code. A similar error occurs in your while() statement.
              >>
              >I change those lines to the following and it worked as expected:
              >>
              > if (strpos($regel, "//!") !== false) {
              >>
              >and
              > } while (!feof($fp) && strpos($regel,' ;') !== false);
              >>
              >P.S. Please don't top post. Thanks.
              >>
              >>
              >>
              The test on a boolean type is certainly the way to go.
              But it doesn't change the unexpected behaviour:
              >
              --------------------------------------------------------------
              code (Corrected):
              --------------------------------------------------------------
              $fp = fopen($filename ,"r");
              $n = 0;
              while ($regel = fgets($fp)) {
              ++$n;
              if (strpos($regel, "//!") !== false) {
              echo $filename,"\n",
              sprintf("%3d ",$n),substr(tr im($regel),4)," \n";
              do {
              $regel = fgets($fp);
              ++$n;
              echo "\t",trim($rege l),"\n";
              if (strpos($regel, ';') !== false) {
              break;
              }
              } while (!feof($fp) );
              echo "\n";
              }
              }
              fclose($fp);
              >
              --------------------------------------------------------------
              Inputfile:
              --------------------------------------------------------------
              <?php
              //! This is working fine (space)
              echo <<< EOT
              A
              B
              C
              EOT;
              //! Unexpected A
              echo <<<EOT
              D
              E
              F
              EOT;
              //! Unexpected B
              echo <<<EOT
              G
              H
              I
              EOT;
              //! Final (space)
              echo <<< EOT
              J
              K
              L
              EOT;
              ?>
              --------------------------------------------------------------
              Output for this file:
              --------------------------------------------------------------
              ./Untitled-1.php
              2 This is working fine (space)
              echo <<< EOT
              A
              B
              C
              EOT;
              >
              ./Untitled-1.php
              8 Unexpected A
              echo <<<<<<< EOT
              J
              K
              L
              EOT;
              >
              >
              ---------------------------------------------------------------
              This is the PHP version:
              PHP Version 5.2.5
              >
              System Windows NT SERVER 5.1 build 2600
              Build Date Nov 8 2007 23:18:08
              Configure Command cscript /nologo configure.js
              "--enable-snapshot-build" "--with-gd=shared"
              Server API Apache 2.0 Handler
              Virtual Directory Support enabled
              Configuration File (php.ini) Path C:\WINDOWS
              Loaded Configuration File C:\testserver\p hp.ini
              PHP API 20041225
              PHP Extension 20060613
              Zend Extension 220060519
              Debug Build no
              Thread Safety enabled
              Zend Memory Manager enabled
              IPv6 Support enabled
              Registered PHP Streams php, file, data, http, ftp, compress.zlib
              Registered Stream Socket Transports tcp, udp
              Registered Stream Filters convert.iconv.* , string.rot13,
              string.toupper, string.tolower, string.strip_ta gs, convert.*, consumed,
              zlib.*
              >
              Jerry Stuckle schreef:
              >
              If you're expecting the PHP code in your input file to be parsed, it
              won't be. All you are doing is reading the file.

              I ran your code with your input file and here's what I got:

              filein.txt
              2 This is working fine (space)
              echo <<< EOT
              A
              B
              C
              EOT;

              filein.txt
              8 Unexpected A
              echo <<<EOT
              D
              E
              F
              EOT;

              filein.txt
              14 Unexpected B
              echo <<<EOT
              G
              H
              I
              EOT;

              filein.txt
              20 Final (space)
              echo <<< EOT
              J
              K
              L
              EOT;

              Which is what I would expect.

              The biggest difference between your php and mine is the level:

              PHP Version =5.2.6

              System =Windows NT MYSYSTEM 5.0 build 2195
              Build Date =May 2 2008 18:01:20
              Configure Command =cscript /nologo configure.js
              "--enable-snapshot-build" "--with-gd=shared"
              "--with-extra-includes=C:\Pro gram Files (x86)\Microsoft
              SDK\Include;C:\ PROGRA~2\MICROS ~2\VC98\ATL\INC LUDE;C:\PROGRA~ 2\MICROS~2\VC98 \INCLUDE;C:\PRO GRA~2\MICROS~2\ VC98\MFC\INCLUD E"
              "--with-extra-libs=C:\Program Files (x86)\Microsoft
              SDK\Lib;C:\PROG RA~2\MICROS~2\V C98\LIB;C:\PROG RA~2\MICROS~2\V C98\MFC\LIB"

              At this point it looks like it might be a bug in PHP 5.2.5, but I don't
              find something similar in the bugs list.

              --
              =============== ===
              Remove the "x" from my email address
              Jerry Stuckle
              JDS Computer Training Corp.
              jstucklex@attgl obal.net
              =============== ===

              Comment

              Working...