How to read a text file in chunks.. 100 lines at a time (\n)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vexus
Forum Newbie
Posts: 8
Joined: Thu Jul 10, 2008 12:33 pm

How to read a text file in chunks.. 100 lines at a time (\n)

Post by vexus »

I have tried numerous different codes and I keep failing at doing this.

Say I have a large text file, each line contains valuable data..so I can't just do a fgets and set a certain amount of data to read because I am not sure on each lines length so some times the data would get cut off and not processed correctly so I need to read it line by line.


The problem is processing each piece of data line by line is way to slow. So i thought if I read say 100 lines at once as a chunk and then processed that chunk of 100 items of data it would speed things up by a lot!


Please some one help me!
vexus
Forum Newbie
Posts: 8
Joined: Thu Jul 10, 2008 12:33 pm

Re: How to read a text file in chunks.. 100 lines at a time (\n)

Post by vexus »

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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: How to read a text file in chunks.. 100 lines at a time (\n)

Post by AbraCadaver »

Depends upon what you mean by "process" the lines. What are you doing with them?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply