如何在 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 次瀏覽

開始你的 職業生涯

透過完成課程獲得認證

開始
廣告