Jump to content

Output last lines from .txt file and reverse output


realWasabi

Recommended Posts

I'm trying to output the last 6 lines of a .txt file in reverse order.

 

So if the file consists of this:

 

line1

line2

line3

line4

line5

line6

line7

line8

line9

line10

 

I wan't it to output:

 

line10

line9

line8

line7

line6

line5

 

I can get it to putput the 6 lines in "normal" order just fine, but are having problems reversing it.

 

This is my code: I tried using $i-- but with no luck:

$file = file("logs/accesslog.txt");
for ($i = count($file)-6; $i < count($file); $i++) {
echo $file[$i] . "<br>"; 
}

I would very much prefer not to use array_reverse() function, as the above script is used in quite a few different functions across my site, and modifying this would mean to modify them all.

 

How could I modify it to reverse the output?

Edited by realWasabi
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.