如何在 PHP 中只读取文本文件最后 5 行?


要只读取文本文件的最后 5 行,代码如下 −

示例

$file = file("filename.txt");
for ($i = max(0, count($file)-6); $i < count($file); $i++) {
   echo $file[$i] . "
"; }

输出

将产生以下输出 −

Given that the file has more than 5 lines of text, the last 5 lines of the text file will be displayed.

打开文件并计算文件中行的数量,然后从最后一行开始读取 5 行。

更新于:09-04-2020

455 次浏览

开始你的 职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.