我有一个这种形式的大文本文件:
This is a linie of text.
This is a linie of text.
This is a linie of text.
This is a linie of text.
This is a linie of text.
This is a linie of text.
This is a linie of text.
This is a linie of text.
This is a linie of text.
等我想在我的网站中从这个文件中输出最后几块/块文本。我目前使用这个:
$line = '';
$f = fopen('output.log', 'r');
$cursor = -1;
fseek($f, $cursor, SEEK_END);
$char = fgetc($f);
/**
* Trim trailing newline chars of the file
*/
while ($char === "\n" || $char === "\r") {
fseek($f, $cursor--, SEEK_END);
$char = fgetc($f);
}
/**
* Read until the start of file or first newline char
*/
while ($char !== false && $char !== "\n" && $char !== "\r") {
/**
* Prepend the new char
*/
$line = $char . $line;
fseek($f, $cursor--, SEEK_END);
$char = fgetc($f);
}
echo $line;
这仅显示最后一行。任何关于这个的想法都会很棒!谢谢!编辑:所有块都用空行分隔,脚本应该打印最后几个块。