note 92406 modified in function.fseek by thiago

From: Date: Thu, 23 Jul 2009 12:24:16 +0000
Subject: note 92406 modified in function.fseek by thiago
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-158622@lists.php.net to get a copy of this message
Modified @ben's function to work for files larger than PHP_INT_MAX bytes. <?php function longTail($file, $numLines = 100) { $fp = fopen($file, "r"); $chunk = 4096; $fs = sprintf("%u", filesize($file)); $max = (intval($fs) == PHP_INT_MAX) ? PHP_INT_MAX : filesize($file); for ($len = 0; $len < $max; $len += $chunk) { $seekSize = ($max - $len > $chunk) ? $chunk : $max - $len; fseek($fp, ($len + $seekSize) * -1, SEEK_END); $data = fread($fp, $seekSize) . $data; if (substr_count($data, "\n") >= $numLines + 1) { preg_match("!(.*?\n){".($numLines)."}$!", $data, $match); fclose($fp); return $match[0]; } } fclose($fp); return $data; } ?> --was-- Modified @ben's function to work for files larger than PHP_INT_MAX bytes. function longTail($file, $numLines = 100) { $fp = fopen($file, "r"); $chunk = 4096; $fs = sprintf("%u", filesize($file)); $max = (intval($fs) == PHP_INT_MAX) ? PHP_INT_MAX : filesize($file); for ($len = 0; $len < $max; $len += $chunk) { $seekSize = ($max - $len > $chunk) ? $chunk : $max - $len; fseek($fp, ($len + $seekSize) * -1, SEEK_END); $data = fread($fp, $seekSize) . $data; if (substr_count($data, "\n") >= $numLines + 1) { preg_match("!(.*?\n){".($numLines)."}$!", $data, $match); fclose($fp); return $match[0]; } } fclose($fp); return $data; } http://php.net/manual/en/function.fseek.php

« previous php.notes (#158622) next »