473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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()?
Aug 14 '08 #1
6 3340
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Maciej Nadolski | last post by:
Hi! I`ve got a problem again:( I almoust finished my project and I was doing stupid forms in html to display as frontend. I have something like that: <?php $wyswietl = ' <html> blahblahblah';...
4
by: Michael G | last post by:
Some of the online docs say that you can use echo <<<END all sorts of text and statements... END; but if the following produces a parse error at line 14 which is the closing
6
by: Paolo Pignatelli | last post by:
I have an aspx code behind page that goes something like this in the HTML view: <asp:HyperLink id=HyperLink1 runat="server" NavigateUrl='<%#"mailto:" &amp;...
7
by: James Johnson | last post by:
Are there structs in JavaScript? If not, what's the closest thing, or do I just use parallel arrays? I'm populating a JavaScript array from ColdFusion query, but I don't think I can do this: ...
9
by: Jon Slaughter | last post by:
I have to output a lot of html stuff mixed with php and I'm curious if its better to echo the html code or stop the php parser temporarily to got into "html mode"? Is there any drawbacks to either...
2
by: paul | last post by:
I have a JS function to change the width of a <divthat works great in Firefox, but not at all in IE7. In IE an error message occurs: Line: 92 Char: 5 Error: Invalid Argument Code: 0 Firefox...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.