Page 1 of 1

how do i read a text file line by line

Posted: Mon Jul 05, 2004 8:27 am
by pelegk2
and not asa whole stirng?

Posted: Mon Jul 05, 2004 8:40 am
by ol4pr0

Code: Select all

$lines ='0';
if ($fh = fopen('textfile.txt','r')) {
while (!feof($fh)) {
if (fgets($fh,1048576)) {
$lines++;
}
}
}
print $lines;
That do the trick ?

Posted: Mon Jul 05, 2004 8:44 am
by launchcode
Is there a specific reason to need to read it line by line? because it takes a lot longer to do. Why not just read it into a string and then split it - or into an array and loop through it?

Posted: Mon Jul 05, 2004 9:22 am
by m3mn0n
file_get_contents()
then explode() by \n or \r\n
foreach() through it all to display it

Posted: Mon Jul 05, 2004 9:23 am
by pickle
It's quite easy actually, just call

Code: Select all

$file_contents = file($filename);
and $file_contents will be an array with each element containing a line of the file.

Posted: Mon Jul 05, 2004 9:25 am
by redmonkey

Posted: Tue Jul 06, 2004 2:31 am
by pelegk2
thnaks all of u
i have used file()
so simple to use

and YEs launchcode there is a reason to read each line by line

Posted: Tue Jul 06, 2004 2:38 am
by launchcode
Ok just wondered - because file() doesn't read it line-by-line at all ;)