realWasabi Posted May 19, 2013 Share Posted May 19, 2013 (edited) 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 May 19, 2013 by realWasabi Quote Link to comment https://forums.phpfreaks.com/topic/278181-output-last-lines-from-txt-file-and-reverse-output/ Share on other sites More sharing options...
ignace Posted May 19, 2013 Share Posted May 19, 2013 $lines = file("logs/accesslog.txt"); for ($pos = count($lines) - 1, $end = $pos - 6; $end < $pos; $pos--) { echo $file[$pos], '<br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/278181-output-last-lines-from-txt-file-and-reverse-output/#findComment-1431051 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.