note 40015 added to function.fgets

From: Date: Thu, 19 Feb 2004 01:54:52 +0000
Subject: note 40015 added to function.fgets
Groups: php.notes 
Request: Send a blank email to php-notes+get-65364@lists.php.net to get a copy of this message
Masar's version is definitely an improvement, but if there is no LF (assuming UNIX line endings) at the end of the last line, this last line will be lost. Here's a new variant, which should have output which exactly matches the input. <?php $handle = fopen("/tmp/inputfile.txt", "r"); if ($handle) { $buffer = fgets($handle, 4096); while (!feof($handle)) { echo $buffer; $buffer = fgets($handle, 4096); } fclose($handle); if (strlen($buffer)) echo $buffer; } ?> Other endings are of course possible, including: <?php // ... if (strlen(trim($buffer))) echo $buffer."\n"; ?> ---- Manual Page -- http://www.php.net/manual/en/function.fgets.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+40015 Delete -- http://master.php.net/manage/user-notes.php?action=delete+40015&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+40015&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#65364) next »