469,271 Members | 1,776 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

reverse reading a text file

xEM
how can i read for example 10 last lines from a text file beginning
from last line? do you have some idea?

in a different manner... how can i set file position indicator one line
upper in text file (line ends with "\n") ?

--
pozdr.
xEM~
Jul 17 '05 #1
5 13818
> how can i read for example 10 last lines from a text file beginning
from last line? do you have some idea?


One way:

$file = array_reverse( file( 'mine.txt' ) );

foreach ( $file as $line ){
echo "$line\n";
}

As for the second question, I'm not sure what you mean.
Jul 17 '05 #2
Sorry, I'm on crack:

$file = array_reverse( file( 'mine.txt' ) );
$limit = 10;

for ($i = 0; $i <= $limit; $i++ ){
echo $file[$i] . "\n";
}

Jul 17 '05 #3
> > how can i read for example 10 last lines from a text file beginning
from last line? do you have some idea?


Oops, you wanted 10 lines:

$file = array_reverse( file( 'mine.txt' ) );
$limit = 10;

for ($i = $limit; $i >= 0; $i-- ){
echo $file[$i] . "\n";
}

Jul 17 '05 #4
Maybe I should test these before I reply?
THIS ONE SPITS OUT TEN LINES :)

$file = array_reverse( file( 'mine.txt' ) );
$limit = 10;

for ($i = 0; $i < $limit; $i++ ){
echo $file[$i] . "\n";
}


Jul 17 '05 #5
On 11 Oct 2003 10:02:59 GMT, xEM <xe**@boxmail.biz> wrote:
how can i read for example 10 last lines from a text file beginning
from last line? do you have some idea?
There's more than one way to do it, but how about:

<pre>
<?php
$file = 'test.txt';

$fp = fopen($file, 'r');

if ($fp) {
$lines = array();

while (($line = fgets($fp)) !== false) {
$lines[] = $line;

while (count($lines) > 10)
array_shift($lines);
}

foreach (array_reverse($lines) as $line) {
print $line;
}

fclose($fp);
}
?>
</pre>

i.e. keep a running buffer of 10 lines (adding lines to the end, and shifting
them off the start if it exceeds 10 lines).

Then you said beginning from last line, so reverse the array before output.
in a different manner... how can i set file position indicator one line
upper in text file (line ends with "\n") ?


Use fseek and fgetc working backwards until you reach the \n you want?

http://uk2.php.net/fseek
http://uk2.php.net/manual/en/function.fgetc.php

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Erik Andersson | last post: by
35 posts views Thread by Raymond Hettinger | last post: by
8 posts views Thread by Jim Langston | last post: by
20 posts views Thread by sahukar praveen | last post: by
4 posts views Thread by Jeff Rodriguez | last post: by
6 posts views Thread by Rajorshi Biswas | last post: by
14 posts views Thread by Faisal Shah | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.