luke777 Posted October 5, 2010 Share Posted October 5, 2010 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 ty Quote Link to comment https://forums.phpfreaks.com/topic/215202-pulling-a-single-line-from-huge-text-file/ Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2010 Share Posted October 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/215202-pulling-a-single-line-from-huge-text-file/#findComment-1119258 Share on other sites More sharing options...
JonnoTheDev Posted October 5, 2010 Share Posted October 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/215202-pulling-a-single-line-from-huge-text-file/#findComment-1119266 Share on other sites More sharing options...
anups Posted October 5, 2010 Share Posted October 5, 2010 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(); Quote Link to comment https://forums.phpfreaks.com/topic/215202-pulling-a-single-line-from-huge-text-file/#findComment-1119285 Share on other sites More sharing options...
luke777 Posted October 5, 2010 Author Share Posted October 5, 2010 Like I said too smart. Thank you all VERY much. especially anups Quote Link to comment https://forums.phpfreaks.com/topic/215202-pulling-a-single-line-from-huge-text-file/#findComment-1119341 Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2010 Share Posted October 5, 2010 SplFileObject is totally new to me. good one, anups. Quote Link to comment https://forums.phpfreaks.com/topic/215202-pulling-a-single-line-from-huge-text-file/#findComment-1119363 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.