我想從舊日誌文件中刪除所有行並保留最下面的50行。如何從日誌文件中保留最後n個行php
我怎樣才能做這樣的事情,如果可能,我們可以改變這些線的方向,
normal input
111111
2222222
3333333
44444444
5555555
output like
555555
4444444
3333333
22222
111111
要看到新鮮的日誌在頂部和只有50條或100線。
如何加入這個?
// set source file name and path
$source = "toi200686.txt";
// read raw text as array
$raw = file($source) or die("Cannot read file");
// join remaining data into string
$data = join('', $raw);
// replace special characters with HTML entities
// replace line breaks with <br />
$html = nl2br(htmlspecialchars($data));
它使輸出成爲HTML文件。那麼你的代碼如何運行呢?
+1雖然當日志文件很大 – Gordon 2010-02-11 22:12:33
非常感謝,但如何與這一個加入吧,他可能會遇到內存問題整個陣列? , //設置源文件名和路徑 $ source =「toi200686.txt」; //將原始文本讀取爲數組 $ raw = file($ source)或die(「Can not read file」); //將剩餘數據加入字符串 $ data = join('',$ raw); //用HTML實體替換特殊字符 //替換換行符與
$ HTML = nl2br(的htmlspecialchars($數據)); 它使輸出成爲html文件,所以你的代碼如何正確運行? 謝謝 – MIkeyy 2010-02-11 22:18:12
我認爲最好在翻轉它之前獲取數組切片,以避免重複可能是一個大數組。 '$ keep = array_reverse(array_slice($ lines,count($ lines) - $ n-1,count($ lines)-1))'。 – 2010-02-11 22:21:41