By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,278 Members | 1,357 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,278 IT Pros & Developers. It's quick & easy.

fgets() and <<<EOT

P: n/a
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()?
Aug 14 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.
js*******@attglobal.net
==================

Aug 14 '08 #2

P: n/a
On Aug 14, 10:26 am, Jerry Stuckle <jstuck...@attglobal.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...@attglobal.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_contents();
Aug 14 '08 #3

P: n/a
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(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 .= <<

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.
Aug 18 '08 #4

P: n/a
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(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.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Aug 19 '08 #5

P: n/a
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(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.
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(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:
Aug 19 '08 #6

P: n/a
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(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.
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(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\MICR OS~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.
js*******@attglobal.net
==================

Aug 20 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.