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!
Reading .txt lines
Moderator: General Moderators
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Reading .txt lines
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];
}mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: Reading .txt lines
I'd use array_slice.
Code: Select all
foreach (array_slice($lines, -5) as $line) {