Zett Posted April 13, 2006 Share Posted April 13, 2006 Hi. I'm wondering if any of you could help me with the algorithm/code for reading a text file [u]line-by-line[/u] in a [u]reverse[/u] order. The delimiter is a newline character "\n".Basically I just want to put the pointer to the end of the file and then read it in a reverse (bottom-up) order.Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/7349-read-file-in-reverse-order/ Share on other sites More sharing options...
kenrbnsn Posted April 13, 2006 Share Posted April 13, 2006 Here's one way of doing it:[list][*]Use the file() function to read the file into an array.[*]Use the array_reverse() function to reverse the array.[*]Use a foreach loop to process each line.[/list]Example:[code]<?php$input = file('input_file.txt');$rev_input = array_reverse($input);foreach ($rev_input as $line) echo trim($line) . '<br>';?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/7349-read-file-in-reverse-order/#findComment-26759 Share on other sites More sharing options...
Zett Posted April 13, 2006 Author Share Posted April 13, 2006 I've tried that and it works well. However, the reason I'm looking for something else is that file() will not be a good option when dealing with large files.I'm working on a guestbook where latest entries are stored at the end of the file. And, of course, latest entries are displayed first. Let's say I have 100 or 200 entries stored in the file, and I only need the last 10. It would be a waste of time and space to copy the whole file into an array and then choose 10 entries. What if there were 1000 or 10 000 entries in the file (I hope you get the idea).I can also simply scan throw the whole file without copying anything until I come to the 10th last entry. From that point to the end I get my 10 latest entries.It would be great if there was a function like fgetrs (like strpos/strrpos) for reading file backwards. Quote Link to comment https://forums.phpfreaks.com/topic/7349-read-file-in-reverse-order/#findComment-26767 Share on other sites More sharing options...
michaellunsford Posted April 13, 2006 Share Posted April 13, 2006 you could write the file in reverse order... that or use a mysql database and the whole problem ceases to exist. Quote Link to comment https://forums.phpfreaks.com/topic/7349-read-file-in-reverse-order/#findComment-26801 Share on other sites More sharing options...
litebearer Posted April 13, 2006 Share Posted April 13, 2006 Hmmmm, ok here comes hammer-man again.1. each entry is placed in its own file. the name of the file is the timestamp of when the file is created.2. place the timestamp/filename into an "index" file 3. when accessing simply place the index file into an array and sort the array(even with 100's of entries still rather quick)4. use an array function to "grab' the 10/20/30/ target timestamps and display them???wadda ya think?Lite... Quote Link to comment https://forums.phpfreaks.com/topic/7349-read-file-in-reverse-order/#findComment-26805 Share on other sites More sharing options...
Zett Posted April 14, 2006 Author Share Posted April 14, 2006 That idea would work for a news system or something similar to that. The problem is that it's a guestbook, sometimes people post a lot of messages like "Nice site", "Hi" and so on. So, a file for each post would be too much.Ok, I found a solution. Actually, it's not a solution, it's a workaround. I'm going to use txtSQL which is a pretty neat thing. It's somewhat a flat-file version of mysql.But the problem of reading the file in reverse order is present and I will trying to find a way to do it. I really want to find the answer because this question arose a while ago but I never really had time to look into this problem. Quote Link to comment https://forums.phpfreaks.com/topic/7349-read-file-in-reverse-order/#findComment-26836 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.