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; 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. 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>"; } ?> 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
Archived
This topic is now archived and is closed to further replies.