note 118733 added to function.fgets
| From: | bfb1985 at yahoo dot es | Date: | Thu, 28 Jan 2016 11:59:07 +0000 |
| Subject: | note 118733 added to function.fgets | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-204786@lists.php.net to get a copy of this message | ||
I had loads of trouble while reading with fgets on a WAMP (Windows server). On local the file went
unto a <pre> tag without a hitch, but when I moved the code to a LAMP production server, every
\r\n created two fgets and I got free empty lines.
I tried deleting with $string=str_replace("\r\n","\n",$string); but it had no
effect whatsoever. The solution was to do an fread() and explode the contents by PHP_EOL and do a
foreach($lines as $line) so every line did not get duplicated.
Here is the example code:
$file=fopen("test.txt,"r");
$text=fread($file,filesize("test.txt"));
$lines=explode(PHP_EOL,$text);
foreach($lines as $line)
{
// Do something
}
----
Server IP: 72.52.91.14
Probable Submitter: 213.37.161.135
----
Manual Page -- http://php.net/manual/en/function.fgets.php
Edit -- https://master.php.net/note/edit/118733
Del: integrated -- https://master.php.net/note/delete/118733/integrated
Del: useless -- https://master.php.net/note/delete/118733/useless
Del: bad code -- https://master.php.net/note/delete/118733/bad+code
Del: spam -- https://master.php.net/note/delete/118733/spam
Del: non-english -- https://master.php.net/note/delete/118733/non-english
Del: in docs -- https://master.php.net/note/delete/118733/in+docs
Del: other reasons-- https://master.php.net/note/delete/118733
Reject -- https://master.php.net/note/reject/118733
Search -- https://master.php.net/manage/user-notes.php