Getting one line in a huge file with PHP

How can I get a specific line in a 3 gig text file. Lines are separated by the \ n character. And I need to be able to get any string on demand.

How can I do that? Only one row needs to be returned. And I would not want to use system calls.

Note. In this case, the question arises of how to do this in bash. I would like to compare it with PHP equiv.

Update: Each line has the same length all the way through.

+7
php
source share
5 answers

Without saving any index in the file, you will need to read it all until you encounter x the number of characters \ n. I see that nickf just posted some way to do this, so I will not repeat it.

To do this several times in an efficient way, you will need to create an index. Store some known file positions for certain (or all) line numbers once, and then you can use them to search in the right place with fseek .

Edit: if each row has the same length, you don't need an index.

$myfile = fopen($fileName, "r"); fseek($myfile, $lineLength * $lineNumber); $line = fgets($myfile); fclose($myfile); 

The line number is 0 in this example, so you might need to subtract it first. The line length includes the \n character.

+8
source share

There is a small discussion of the problem, and it is not mentioned how to refer to the "one line" (by number, by some value inside it, etc.), so below is just a hunch about what you want.

If you are not averse to using an object (perhaps it may be a "too high level") and want to refer to a string by offset, then SplFileObject (available since PHP 5.1.0). See the following basic example:

 $file = new SplFileObject('myreallyhugefile.dat'); $file->seek(12345689); // seek to line 123456790 echo $file->current(); // or simply, echo $file 

This particular ( seek ) method requires scanning through a file in turn. However, if, as you say, all the lines are the same length, you can use fseek instead to get where you want to go much faster.

 $line_length = 1024; // each line is 1 KB line $file->fseek($line_length * 1234567); // seek lots of bytes echo $file->current(); // echo line 1234568 
+8
source share

The only way I can do this would be as follows:

 function getLine($fileName, $num) { $fh = fopen($fileName, 'r'); for ($i = 0; $i < $num && ($line = fgets($fh)); ++$i); return $line; } 
+1
source share

You said that each line has the same length, so you can use fopen () in combination with fseek () to get the line quickly.

http://ch2.php.net/manual/en/function.fseek.php

+1
source share

Although this is not a solution exactly, you need to pull one line from a 3 gigabyte text file? is it a problem, or can it go leisurely? If you need to pull many lines from this file at different points in time, I would definitely suggest putting this data in some kind of database. SQLite, maybe your friend here, is very simple, but not great, with many scripts / people accessing it at a time.

0
source share

All Articles