public static function readBySeek($filepath,$lines,$revers = false){
$offset = -1;
$c = '';
$read = '';
$i = 0;
$fp = fopen($filepath, "r");
while( $lines && fseek($fp, $offset, SEEK_END) >= 0 ) {
$c = fgetc($fp);
if($c == "\n" || $c == "\r"){
$lines--;
if( $revers ){
$read[$i] = strrev($read[$i]);
$i++;
}
}
if( $revers ) $read[$i] .= $c;
else $read .= $c;
$offset--;
}
fclose($fp);
if( $revers ){
if($read[$i] == "\n" || $read[$i] == "\r")
array_pop($read);
else $read[$i] = strrev($read[$i]); //反转字符串
return implode('',$read);
}
return strrev(rtrim($read,"\n\r"));
}php fseek读取文件(从后往前读取)
最新推荐文章于 2025-05-22 10:38:35 发布
本文介绍了一种使用PHP实现的从文件末尾逆向读取指定行数的方法。该方法通过调整文件指针的位置来逐字符逆向读取,并支持正常顺序及逆序输出。适用于日志文件等大型文本文件的高效读取。
&spm=1001.2101.3001.5002&articleId=76515696&d=1&t=3&u=ceb36640ea494a26922dfa972222b178)
443

被折叠的 条评论
为什么被折叠?



