使用 fseek 逐行向后读取文件
如何使用 fseek 逐行向后读取文件?
代码可能会有所帮助。必须是跨平台的、纯php的。
提前非常
感
谢杰拉
How do I read a file backwards line by line using fseek?
code can be helpful. must be cross platform and pure php.
many thanks in advance
regards
Jera
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
问题是使用 fseek 询问,因此只能假设性能是一个问题,而 file() 不是解决方案。这是使用 fseek 的简单方法:
My file.txt
和代码:
输出:
您不必像我一样附加到 $lines 数组,如果这是脚本的目的,您可以立即打印输出。如果您想限制行数,引入计数器也很容易。
The question is asking using fseek, so can only assume that performance is an issue and file() is not the solution. Here is a simple approach using fseek:
My file.txt
And the code:
Output:
You don't have to append to the $lines array like I am, you can print the output straight away if that is the purpose of your script. Also it is easy to introduce a counter if you want to limit the number of lines.
如果您要以任何方式读取整个文件,只需使用 file() 将文件读取为一个数组(每一行是数组中的每个元素),然后使用 array_reverse() 向后翻转数组并循环遍历它。或者只是做一个反向 for 循环,从末尾开始并在每个循环上递减。
If you are going to read the entire file in anyways, just use file() to read the file in as an array (each line is each element in the array) and then use array_reverse() to flip the array backwards and loop through that. Or just do a reverse for loop where you start at the end and decrement on each loop.
要完全反转文件:
当然,您想要逐行反转...
不过,实际上,乔纳森·库恩(Jonathan Kuhn)在上面给出了最好的答案。据我所知,您不会使用他的答案的唯一情况是,如果通过 php.ini 禁用了
file
或类似功能,但管理员忘记了 fseek,或者在打开一个大文件时只需获取最后几行内容会通过这种方式神奇地节省内存。注意:不包括错误处理。而且,PHP_EOL 不配合,所以我用“\n”来表示行尾。因此,上述方法可能并不适用于所有情况。
To completely reverse a file:
Of course, you wanted line-by-line reversal...
Really though, Jonathan Kuhn has the best answer IMHO above. The only cases you'd not use his answer that I know of is if
file
or like functions are disabled via php.ini, yet the admin forgot about fseek, or when opening a huge file just get the last few lines of contents would magically save memory this way.Note: Error handling not included. And, PHP_EOL didn't cooperate, so I used "\n" to denote end of line instead. So, above may not work in all cases.
您无法逐行进行 fseek,因为在阅读之前您不知道这些行有多长。
您应该将整个文件读入行列表,或者如果文件太大而您只需要最后几行,则从文件末尾读取固定大小的块并实现更复杂的逻辑来检测来自此类数据的行。
You cannot fseek line by line, because you do not know how long the lines are until you read them.
You should either read the whole file into a list of lines, or if the file is too big for that and you only need the last lines, read fixed-sized chunks from the end of the file and implement a bit more complicated logic which detects lines from such data.
将整个文件读入数组并反转是可以的,除非文件很大。
您可以从后到前对文件执行缓冲读取,如下所示:
Reading the entire file into an array and reversing is fine unless the file is enormous.
You could perform a buffered read of your file from back to front with something like this:
此代码向后读取文件。此代码忽略读取时的修改,例如 apache access.log 处理时的新行。
This code read file backwards. This code ignore modifications on reading, example apache access.log new lines on procressing.
这里有一个 fgets($fp) 的替换(ish),称为 fgetsr(),它以相反的顺序从文件中读取行。
该代码是逐字记录的,因此您应该(著名的最后一句话)能够将其复制到服务器上的文件中并运行它。尽管您很可能需要更改 fopn() 调用中的文件名。
Here's a drop in replacement(ish) for fgets($fp) called fgetsr() that reads lines from a file in reverse order.
This code is verbatim so you should (famous last words) be able to copy it into a file on your server and run it. Though you may well need to change the filename in the fopn() call.
反向逐行读取文件的函数:
示例用例:解析长文件直到某个日期:
Functions to read a file line-by-line in reverse:
Example use case: parse a long file until some date:
我知道这个问题已经得到解答,但我找到了另一种可能更快的方法。
I know this has been answered already but I found another, maybe faster, way.