Jump to content

Pulling a single line from HUGE text file


luke777

Recommended Posts

I'm ok with PHP but probably not half as good as some of you guys on here.

 

I am basically trying to find a way to grab a line from a huge and I mean huge text file.... its basically a list of keywords I want to call by line number but without preferably going through them all before I get to that line.....otherwise couldmcrash my server obviously.

 

At the moment im using this

 

$lines = file('http://www.mysite.com/keywords.txt');

// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
echo "$line_num";
}

 

This works but im sure theres gotta be a better way of doing to save on usuage because this is putting the whole file into the memory and if I can simply say to php give me line number 97, would umm RULE....

 

Hope you guys can come up with a solution as your much smarter than me :P ty

Link to comment
Share on other sites

As you've noted, you do not want to read an entire huge file into memory using the file() function. i suggest that you fopen() the file and fread() lines individually to find what you're looking for. You can say 'give me line 97', but it will take more code than that, maybe looping via fread() until you get there.

Link to comment
Share on other sites

You can use fread() to read portions of the file until what you are looking for has been found.

http://uk2.php.net/fread

 

You could import the data from the text file into a database table and use queries to search for required data.

 

Or, you could use a full text search engine such as sphinx http://sphinxsearch.com/ to generate an index of your text file giving you the ability to perform super fast searches.

 

It really depends on the size of the file in question. If it is megabytes then the first 2 options are valid. If it is hundreds of megabytes then the last option would be my choice.

Link to comment
Share on other sites

use SplFileObject

      $file = "test.txt";
      $line_number = 1000;
      $file_obj = new SplFileObject( $file );
        /*** seek to the line number ***/
      $file_obj->seek( $line_number );

       /*** return the current line ***/
       echo  $file_obj->current();

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.