note 40345 deleted from function.fgets by nlopess

From: Date: Mon, 01 Mar 2004 14:25:23 +0000
Subject: note 40345 deleted from function.fgets by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-66046@lists.php.net to get a copy of this message
Note Submitter: stewey@ambitious.ca ---- 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; } ?>

« previous php.notes (#66046) next »