开发者

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"; 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜