I have a large text file in this form:
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.
etc. I want to output the last couple of chunks/blocks of text from this file in my website. I currently use this:
$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;
That shows just the last line. Any thought about this would be awesome! Thanks! Edit: All blocks are separated by empty lines, the script should print the last few blocks.