Jump to content


Photo

Output last lines from .txt file and reverse output


  • Please log in to reply
1 reply to this topic

#1 realWasabi

realWasabi

    Member

  • Members
  • PipPip
  • 23 posts

Posted 19 May 2013 - 02:22 PM

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, 19 May 2013 - 02:25 PM.


#2 ignace

ignace

    Now mod flavored

  • Moderators
  • 6,239 posts
  • LocationBelgium

Posted 19 May 2013 - 02:40 PM

$lines = file("logs/accesslog.txt");

for ($pos = count($lines) - 1, $end = $pos - 6; $end < $pos; $pos--) {
  echo $file[$pos], '<br>';
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com