frist44 Posted February 20, 2009 Share Posted February 20, 2009 I'm reading in text from a syslog file. I would like the newest lines to read at the top. What's the best way to reverse the order of the contents? Here's what I have so far: $filename = "file.log"; $fd = fopen ($filename, "r"); $contents = fread ($fd,filesize ($filename)); fclose ($fd); $linedcontents = nl2br($contents); echo $linedcontents; Quote Link to comment https://forums.phpfreaks.com/topic/146123-reverse-order-of-read-input/ Share on other sites More sharing options...
premiso Posted February 20, 2009 Share Posted February 20, 2009 If you are not writing to the log use file then rsort to reverse the order of the array, then you can implode using "\n" at the glue and it should work fine. (Or use a foreach to display the results. Quote Link to comment https://forums.phpfreaks.com/topic/146123-reverse-order-of-read-input/#findComment-767123 Share on other sites More sharing options...
frist44 Posted February 20, 2009 Author Share Posted February 20, 2009 Perfect. Thanks. <?php $file = file("file.log"); $file = array_reverse($file); foreach ($file as $line) { echo $line . "</br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/146123-reverse-order-of-read-input/#findComment-767131 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.