vexusdev Posted February 11, 2010 Share Posted February 11, 2010 I have tried numerous different codes and I can't seem to get this working right. I want to read a text file say 100 lines at a time. If you guys need a little bit of clarification. say I have a text file with Nick Bob John Larry Ashley Britney Tom Lisa Joshua Say I need to process that list 2 lines at a time. First chunk [Nick,Bob] Second chunk [John,Larry] Third chunk [Ashley,Britney] Does anyone have the slightest idea of how to accomplish that? Quote Link to comment Share on other sites More sharing options...
sader Posted February 11, 2010 Share Posted February 11, 2010 $lines = file('file.txt'); for($i=0; $i<2; $i++) { echo $lines[$i]; } Quote Link to comment Share on other sites More sharing options...
vexusdev Posted February 11, 2010 Author Share Posted February 11, 2010 $lines = file('file.txt'); for($i=0; $i<2; $i++) { echo $lines[$i]; } Yeh but that sort of rules out large files.. file() has size limits and its not efficient because we are loading the whole file into the memory right off the bat. Quote Link to comment Share on other sites More sharing options...
sader Posted February 11, 2010 Share Posted February 11, 2010 if u have huge mounts of data why not use databases? Quote Link to comment Share on other sites More sharing options...
vexusdev Posted February 11, 2010 Author Share Posted February 11, 2010 if u have huge mounts of data why not use databases? Well because the user will be uploading a list and this list will be converted(by querying a database) and then rewriting to another text file. Also the code that you wrote.. won't that only read the first 2 lines of the file? I need to read the whole file 2 lines at a time. I just can't grasp how to accomplish this because of my limited knowledge. Quote Link to comment Share on other sites More sharing options...
Mchl Posted February 11, 2010 Share Posted February 11, 2010 $fh = fopen('filename','rb'); while(!feof($fh)) { $line1 = fgets($fh); if(!feof($fh)) {$line2 = fgets($fh);} //do something with these two lines } [edited] Its fgets obviously, not fread Quote Link to comment Share on other sites More sharing options...
vexusdev Posted February 11, 2010 Author Share Posted February 11, 2010 $fh = fopen('filename','rb'); while(!feof($fh)) { $line1 = fgets($fh); if(!feof($fh)) {$line2 = fgets($fh);} //do something with these two lines } [edited] Its fgets obviously, not fread Thank you, say I wanted to expand that to read 100 lines at a time is it possible? What if i made a loop from 1 to 100 with fgets inside the while loop and insert the data into an array? Quote Link to comment Share on other sites More sharing options...
Mchl Posted February 11, 2010 Share Posted February 11, 2010 Of course you can do it like that. Quote Link to comment Share on other sites More sharing options...
vexusdev Posted February 11, 2010 Author Share Posted February 11, 2010 Of course you can do it like that. One more question. say i did this: while fgets fgets fgets Thats not actual code of course but would that grab the line 3 times right then? then next while rotation be at 4.. ready to grab 4,5,6? Quote Link to comment Share on other sites More sharing options...
Mchl Posted February 11, 2010 Share Posted February 11, 2010 Yes Quote Link to comment Share on other sites More sharing options...
vexusdev Posted February 11, 2010 Author Share Posted February 11, 2010 Yes Thank you so much your a star! Quote Link to comment Share on other sites More sharing options...
vexusdev Posted February 11, 2010 Author Share Posted February 11, 2010 Shoot I knew I would be back.. any idea why this doesn't output anything? edit.. stupid msitake fixed it! Quote Link to comment 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.