Jump to content

Archived

This topic is now archived and is closed to further replies.

Zett

Read file in reverse order

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
you could write the file in reverse order... that or use a mysql database and the whole problem ceases to exist.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.