如何在 PHP 中读取文本文件的最后 5 行?
phpserver side programmingprogramming更新于 2025/6/27 13:37:17
要读取文本文件的最后 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 行。