How to read a file from line x to line y (with php)

妖精的绣舞 提交于 2019-12-25 04:06:28

问题


I've explored all over the internet to find a solution; but all of them are neglecting an important issue. The best solution was in Stack Overflow as

  $file = new SplFileObject('longFile.txt');
    $fileIterator = new LimitIterator($file, 1000, 2000);
    foreach($fileIterator as $line) {
    echo $line, PHP_EOL;
    } 

But like other approaches, this needs to read from the beginning of the file to reach the offset line. Usually, it is negligible; but for large files (say millions of line), this significantly slow down the process. The time increases monotonically by the increase of the offset. If you put the offset at millions, the process time will be few seconds.

In databases (like mysql), we index the table to read a row without walking through the whole database. Is there to do such thing with the file key (line number)? I wonder how flat file databases like SQLite and Berkeley DB do index their tables.


回答1:


The conceptual problem here is that files are just strings of characters, some of those characters denote ends of lines. Because of that, it is impossible to know where lines begin and end without reading the file first.

If a file is to be read constantly, you scan the file first and record the offsets for the lines into some kind of index and use fseek() and fread() to read exactly the lines you want.

As you mentioned, databases can do a similar job for you so, instead of creating, essentially, your own database, you could read the file line–by–line and insert those lines in database with some field storing the line number and then get the lines you want with a query.




回答2:


There is no way to seek to a particular line, because "line" term is just a convention. Line is a "set of characters separated by \n". And file has no idea about this convention. So to get N'th line you need to traverse char by char to count needed amount of lines.

As you mentioned - you can improve the performance using some sort of custom created index (like row number - offset in bytes list), but to build it you need to parse the file anyway.




回答3:


<?php

    $strings = file_get_contents($file);

    $length= strlen($strings);

    for($i=0;$i<$length;$i++) {
        print $strings{$i};
    }

?>

The above code will get file contents in strings, and then will iterate each character one by one, now its upto you how you want to make use of them.



来源:https://stackoverflow.com/questions/7411961/how-to-read-a-file-from-line-x-to-line-y-with-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!