我想删除旧日志文件中的所有行,并保留最底部的50行.
我该怎么做这样的事情,如果可能的话,我们可以改变这些线的方向,
normal input
111111
2222222
3333333
44444444
5555555
output like
555555
4444444
3333333
22222
111111
Run Code Online (Sandbox Code Playgroud)
要查看顶部的新鲜日志,仅查看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));
Run Code Online (Sandbox Code Playgroud)
它将输出作为HTML文件.那你的代码将如何运行呢?
$lines = file('/path/to/file.log'); // reads the file into an array by line
$flipped = array_reverse($lines); // reverse the order of the array
$keep = array_slice($flipped,0, 50); // keep the first 50 elements of the array
Run Code Online (Sandbox Code Playgroud)
从那里你可以用任何东西$keep.例如,如果你想把它吐出来:
echo implode("\n", $keep);
Run Code Online (Sandbox Code Playgroud)
要么
file_put_contents('/path/to/file.log', implode("\n", $keep));
Run Code Online (Sandbox Code Playgroud)