php 获取文件最后几行,PHP从文件中读取最后几行的最佳方法是什么?

这是修改后的版本,也可以跳过最后几行:

/**

* Modified version of http://www.geekality.net/2011/05/28/php-tail-tackling-large-files/ and of https://gist.github.com/lorenzos/1711e81a9162320fde20

* @author Kinga the Witch (Trans-dating.com), Torleif Berger, Lorenzo Stanco

* @link http://stackoverflow.com/a/15025877/995958

* @license http://creativecommons.org/licenses/by/3.0/

*/

function tailWithSkip($filepath, $lines = 1, $skip = 0, $adaptive = true)

{

// Open file

$f = @fopen($filepath, "rb");

if (@flock($f, LOCK_SH) === false) return false;

if ($f === false) return false;

if (!$adaptive) $buffer = 4096;

else {

// Sets buffer size, according to the number of lines to retrieve.

// This gives a performance boost when reading a few lines from the file.

$max=max($lines, $skip);

$buffer = ($max < 2 ? 64 : ($max < 10 ? 512 : 4096));

}

// Jump to last character

fseek($f, -1, SEEK_END);

// Read it and adjust line number if necessary

// (Otherwise the result would be wrong if file doesn't end with a blank line)

if (fread($f, 1) == "\n") {

if ($skip > 0) { $skip++; $lines--; }

} else {

$lines--;

}

// Start reading

$output = '';

$chunk = '';

// While we would like more

while (ftell($f) > 0 && $lines >= 0) {

// Figure out how far back we should jump

$seek = min(ftell($f), $buffer);

// Do the jump (backwards, relative to where we are)

fseek($f, -$seek, SEEK_CUR);

// Read a chunk

$chunk = fread($f, $seek);

// Calculate chunk parameters

$count = substr_count($chunk, "\n");

$strlen = mb_strlen($chunk, '8bit');

// Move the file pointer

fseek($f, -$strlen, SEEK_CUR);

if ($skip > 0) { // There are some lines to skip

if ($skip > $count) { $skip -= $count; $chunk=''; } // Chunk contains less new line symbols than

else {

$pos = 0;

while ($skip > 0) {

if ($pos > 0) $offset = $pos - $strlen - 1; // Calculate the offset - NEGATIVE position of last new line symbol

else $offset=0; // First search (without offset)

$pos = strrpos($chunk, "\n", $offset); // Search for last (including offset) new line symbol

if ($pos !== false) $skip--; // Found new line symbol - skip the line

else break; // "else break;" - Protection against infinite loop (just in case)

}

$chunk=substr($chunk, 0, $pos); // Truncated chunk

$count=substr_count($chunk, "\n"); // Count new line symbols in truncated chunk

}

}

if (strlen($chunk) > 0) {

// Add chunk to the output

$output = $chunk . $output;

// Decrease our line counter

$lines -= $count;

}

}

// While we have too many lines

// (Because of buffer size we might have read too many)

while ($lines++ < 0) {

// Find first newline and remove all text before that

$output = substr($output, strpos($output, "\n") + 1);

}

// Close file and return

@flock($f, LOCK_UN);

fclose($f);

return trim($output);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值