note 106336 added to function.fseek

From: Date: Fri, 28 Oct 2011 16:06:58 +0000
Subject: note 106336 added to function.fseek
Groups: php.notes 
Request: Send a blank email to php-notes+get-184201@lists.php.net to get a copy of this message
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! ;-) ---- Server IP: 79.132.192.3 Probable Submitter: 95.168.183.161 ---- Manual Page -- http://www.php.net/manual/en/function.fseek.php Edit -- https://master.php.net/note/edit/106336 Del: integrated -- https://master.php.net/note/delete/106336/integrated Del: useless -- https://master.php.net/note/delete/106336/useless Del: bad code -- https://master.php.net/note/delete/106336/bad+code Del: spam -- https://master.php.net/note/delete/106336/spam Del: non-english -- https://master.php.net/note/delete/106336/non-english Del: in docs -- https://master.php.net/note/delete/106336/in+docs Del: other reasons-- https://master.php.net/note/delete/106336 Reject -- https://master.php.net/note/reject/106336 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#184201) next »