Page 1 of 1

Reading .txt lines

Posted: Sat Sep 11, 2010 9:56 pm
by ManiZach
Hi! I need help with creating a small snippet... I want a code that reads the 5 last lines of a txt file (Reading the newest items added only.) How can this be done?

Thanks!

Re: Reading .txt lines

Posted: Sat Sep 11, 2010 11:10 pm
by AbraCadaver
Probably a better way, but I'm off to bed:

Code: Select all

$lines = file('/path/to/file.txt');
for($i=count($lines)-5; $i<count($lines); $i++) {
  echo $lines[$i];
}

Re: Reading .txt lines

Posted: Sat Sep 11, 2010 11:28 pm
by requinix
I'd use array_slice.

Code: Select all

foreach (array_slice($lines, -5) as $line) {