note 40480 added to function.fgets
| From: | stewey at ambitious dot ca | Date: | Thu, 04 Mar 2004 22:55:44 +0000 |
| Subject: | note 40480 added to function.fgets | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-66278@lists.php.net to get a copy of this message | ||
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;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.fgets.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+40480
Delete -- http://master.php.net/manage/user-notes.php?action=delete+40480&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+40480&report=yes
Search -- http://master.php.net/manage/user-notes.php