PHP 8.5.0 Beta 1 available for testing

Voting

: max(nine, three)?
(Example: nine)

The Note You're Voting On

steve at studio831 dot com
16 years ago
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;
}
?>

<< Back to user notes page

To Top