note 40345 added to function.fgets

From: Date: Sun, 29 Feb 2004 21:54:15 +0000
Subject: note 40345 added to function.fgets
Groups: php.notes 
Request: Send a blank email to php-notes+get-66007@lists.php.net to get a copy of this message
This is an update to the user defined getLine function. This update does several things differently. 1. Includes the line ending in the returned string. 2. Addresses a bug in the first posting whereby windows '\r\n' line endings resulted in two lines being returned. 3. Addresses a bug in the second posting by 'horror at hack dot gr' that caused '\n\r' to be interpreted as a single line. '\n\r' is not a known line ending. 4. Preserves the PHPDoc/Pear style coding conventions employed in the original post. Here it is: <?php /** * GetLine * Obtain the next line in a given file by reading each character until a * \r, \n, or \r\n is reached. * * @param file a handle returned from fopen for our file. * @return string the next line in the file. */ function getLine($file) { // iterate over each character in line. while ($character = fgetc($file)) { // append the character to the buffer. $buffer .= $character; // check for end of line. if (($character == "\n") or ($character == "\r")) { // checks if the next character is part of the line ending, // as in the case of windows '\r\n' files, or not as in the // case of mac classic '\r', and unix/os x '\n' files. $character = fgetc($file); if ($character == "\n") { // part of line ending, append to buffer. $buffer .= $character; } else { // not part of line ending, roll back file pointer. fseek($file, -1, SEEK_CUR); } // end of line, so stop reading. break; } } // return the line buffer. return $buffer; } ?> ---- Manual Page -- http://www.php.net/manual/en/function.fgets.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+40345 Delete -- http://master.php.net/manage/user-notes.php?action=delete+40345&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+40345&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#66007) next »