您的位置:首页 > 编程语言 > PHP开发

php 倒序读取txt 文件中最后几行的内容,带分页

2013-12-18 00:00 1126 查看
摘要: php 倒序读取txt 文件中最后几行的内容,带分页

// $filename: 文件名
// $start: 从倒数第几行开始
// $lines: 读取几行
function read_backward_line($filename, $start, $lines){
$lines++;
$offset = -1;
$c = '';
$read = '';
$i = 0;
$fp = @fopen($filename, "r");

$tmpStart = 0;
while( $lines && fseek($fp, $offset, SEEK_END) >= 0 ) {

$c = fgetc($fp);
if($c == "\n" || $c == "\r"){
if(++$tmpStart >= $start)
$lines--;
}

if($tmpStart >= $start)
$read .= $c;
$offset--;
}

$read = trim($read);

$read = array_reverse(explode("\n",strrev($read)));
foreach($read as $key => $value) {
if (empty($value)) {
unset($read[$key]);
}
}
return $read;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php