Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Grab last 15 lines in txt file

Thank you for taking the time to read this and I will appreciate every single response no mater the quality of content. :)

Using PHP, I'm trying to get the last 15 lines of a text document (.txt) and store that data into a php variable. I understand that this is possible, however when I do get the last 15 lines, is it possible to retain the order? For example:

text document:

A
B
C

When I grab the text document from the last 15 characters, I don't want the echo to end up like:

C
B
A

All assistance is appreciated and I look forward to your replies; thank you. :) If I didn't explain anything clearly and/or you'd like me to explain in more detail, please reply. :)

Thank you.

like image 359
aullah Avatar asked Nov 30 '22 10:11

aullah


1 Answers

Try using array_slice, which will return a part of an array. In this case you want it to return the last 15 lines of the array, so:

$filearray = file("filename");
$lastfifteenlines = array_slice($filearray,-15);
like image 194
Bob Baddeley Avatar answered Dec 19 '22 04:12

Bob Baddeley