Function to get certain amount of lines from a file?
Was just wondering if there is anything that exists to retrieve a certain amount of lines from a file or should I just go ahead & make an array of it with file
& then loop through it until I get the required data?
All I开发者_运维问答'm trying to do is get the first 11 lines of data from a file (or less if there aren't 11).
For such a small amount of lines, I'd recommend fopen()
. This way for big files you haven't read in the whole thing.
Example:
$fp = fopen('somefile.txt', 'r');
$i = 0;
while ( ($line = fgets($fp)) !== false && $i < 12 ) {
++$i;
echo "$i: $line<br>";
}
fclose($fp);
Read more about fgets()
You could try fgets to get the line in the cycle.
$file = "test.txt";
$lines = count(file($file));
echo "There are $lines lines in $file";
精彩评论