如何在 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 行。
廣告