note 40480 deleted from function.fgets by nlopess
| From: | nlopess@php.net | Date: | Wed, 14 Apr 2004 14:23:43 +0000 |
| Subject: | note 40480 deleted from function.fgets by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-68117@lists.php.net to get a copy of this message | ||
Note Submitter: stewey@ambitious.ca
----
An alternative to fgets for those of us without 'auto_detect_line_endings' support.
<?php
/**
* GetLine
* Obtain the next line in a given file by reading each character until a
* \r or \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 (!feof($file))
{
// append the character to the buffer.
$character = fgetc($file);
$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;
}
?>