note 50297 deleted from function.fgets by nlopess
| From: | nlopess@php.net | Date: | Fri, 25 Feb 2005 17:22:29 +0000 |
| Subject: | note 50297 deleted from function.fgets by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-85562@lists.php.net to get a copy of this message | ||
Note Submitter: purevel at hotmail dot com
----
I couldn't find any code to read just a single, specified line of a file, so I wrote this.
<?php
function read_line($datafile,$linenum)
{
$file = fopen($datafile, 'r');
$i=1;
while ($i <= $linenum)
{
$line=""; $line = fgets($file);
$i++;
}
echo $line;
fclose($file);
}
?>
The only inputs required are the filename of the file being opened, and the line number.
For example using:
<?php
echo read_line('sample',2)
?>
will read the second line of the file "sample"