note 59580 added to function.fseek

From: Date: Sat, 10 Dec 2005 02:46:21 +0000
Subject: note 59580 added to function.fseek
Groups: php.notes 
Request: Send a blank email to php-notes+get-99883@lists.php.net to get a copy of this message
If you, like me, need the second to last line from a text file (or some other line near the end that you will know the number of, ie the fourth to last line) then this addition to Jim's code can help you. //$linefromlast is the linenumber that you need, the last line being 1, the second to last being 2, and so on... function readlog($file, $linefromlast){ $fp = @fopen($file, "r"); $pos = -2; $t = " "; $linecounter = 1; while ($t != "\n" and $linecounter<=$linefromlast) { fseek($fp, $pos, SEEK_END); $t = fgetc($fp); $pos = $pos - 1; if ($t == "\n" and $linecounter < $linefromlast) { fseek($fp, $pos, SEEK_END); $t = fgetc($fp); $pos = $pos - 1; $linecounter = $linecounter +1; } } $t = fgets($fp); fclose($fp); return $t; } ---- Manual Page -- http://www.php.net/manual/en/function.fseek.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+59580 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59580&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59580&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59580&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59580&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59580&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59580&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59580&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+59580&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#99883) next »