Reputation: 6239
while($c=fgets(STDIN)){
if($c===PHP_EOL){
continue;
}
else {
echo $c;
}
}
When the above code is called from the command line in php 5.3.8, it prints the rows in the input file, and skips any empty lines. However, in php 5.2.6, it does not skip over the empty lines.
Is there any way of changing the code to make it skip over empty lines in the lder versions as well?
Upvotes: 1
Views: 156
Reputation: 57650
If you change the condition to
if(ord($c[0])==0x0a || ord($c[0])==0x0d)
This should work on any platform.
See ord
Upvotes: 1
Reputation: 20997
If you don't need trailing spaces, you can use:
$c = rtrim( $c, "\n\r");
if( $c == ''){
continue;
}
if you do, you should probably check string length or use this:
if( ($c{0} == "\n") || ($c{0} == "\r")){
continue;
}
Upvotes: 0
Reputation: 5823
The difference probably caused by php.ini change, not the version. Check auto_detect_line_endings
key in your php config file or if you have the access try;
<?php
ini_set("auto_detect_line_endings", true);
?>
auto_detect_line_endings
is used by fgets
. PHP_EOL
always gets the correct end of line character depending on the OS but fgets
may fail if you don't set auto_detect_line_endings
to true.
Upvotes: 2
Reputation: 1300
$c = str_replace("\r\n", "", $c);
P.S. I don't think that php version is significant. It seems like something wrong with configuration.
Upvotes: 0