ConFoo Montreal 2026: Call for Papers

Voting

: min(seven, three)?
(Example: nine)

The Note You're Voting On

kavoshgar3 at gmail dot com
13 years ago
sometimes we want read file from last line to beginning of file.I use the following.
<?php
function read_backward_line($filename, $lines, $revers = false)
{
$offset = -1;
$c = '';
$read = '';
$i = 0;
$fp = @fopen($filename, "r");
while(
$lines && fseek($fp, $offset, SEEK_END) >= 0 ) {
$c = fgetc($fp);
if(
$c == "\n" || $c == "\r"){
$lines--;
if(
$revers ){
$read[$i] = strrev($read[$i]);
$i++;
}
}
if(
$revers ) $read[$i] .= $c;
else
$read .= $c;
$offset--;
}
fclose ($fp);
if(
$revers ){
if(
$read[$i] == "\n" || $read[$i] == "\r")
array_pop($read);
else
$read[$i] = strrev($read[$i]);
return
implode('',$read);
}
return
strrev(rtrim($read,"\n\r"));
}
//if $revers=false function return->
//line 1000: i am line of 1000
//line 1001: and i am line of 1001
//line 1002: and i am last line
//but if $revers=true function return->
//line 1002: and i am last line
//line 1001: and i am line of 1001
//line 1000: i am line of 1000
?>
Enjoy! Mail me if it works! ;-)

<< Back to user notes page

To Top