Re: fgets() and <<<EOT
- From: Jerry Stuckle <jstucklex@xxxxxxxxxxxxx>
- Date: Tue, 19 Aug 2008 21:04:58 -0400
Gert Kok wrote:
Gert Kok wrote:The test on a boolean type is certainly the way to go.Jerry Stuckle schreef:> This is the reading part: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.
>
> $fp = fopen($filename,"r");
> $n = 0;
> while ($regel = fgets($fp)) {
> ++$n;
> if (strpos($regel,"//!")) {
> echo $filename,"\n",
> sprintf("%3d ",$n),substr(trim($regel),4),"\n";
> do {
> $regel = fgets($fp);
> ++$n;
> echo "\t",trim($regel),"\n";
> } while (!feof($fp) && !strpos($regel,';'));
> echo "\n";
> }
> }
> fclose($fp);
>
>
>
> This was in the input file:
>
> //! Bevestigingsmail: 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.
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(trim($regel),4),"\n";
do {
$regel = fgets($fp);
++$n;
echo "\t",trim($regel),"\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\php.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_tags, 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:\Program Files (x86)\Microsoft SDK\Include;C:\PROGRA~2\MICROS~2\VC98\ATL\INCLUDE;C:\PROGRA~2\MICROS~2\VC98\INCLUDE;C:\PROGRA~2\MICROS~2\VC98\MFC\INCLUDE" "--with-extra-libs=C:\Program Files (x86)\Microsoft SDK\Lib;C:\PROGRA~2\MICROS~2\VC98\LIB;C:\PROGRA~2\MICROS~2\VC98\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@xxxxxxxxxxxxx
==================
.
- References:
- fgets() and <<<EOT
- From: Gert Kok
- Re: fgets() and <<<EOT
- From: Jerry Stuckle
- Re: fgets() and <<<EOT
- From: Gert Kok
- Re: fgets() and <<<EOT
- From: Jerry Stuckle
- Re: fgets() and <<<EOT
- From: Gert Kok
- fgets() and <<<EOT
- Prev by Date: Re: Memory limit troubles on XP(sp3) with PHP5.2.6
- Next by Date: Re: Most Viewed PHP Tutorial
- Previous by thread: Re: fgets() and <<<EOT
- Next by thread: any php/css parsers out there?
- Index(es):
Relevant Pages
|