472,133 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,133 software developers and data experts.

read line from text file

I have a text file that i would like to use PHP to read from. each line
ends with a \n. i need to read from the beginning of the line, only upto
that \n
right now i'm using this, which is sloppy and slow:

if ($chr = fgetc($file)) != "\n") { }

which of course reads in one byte at a time till the end of line. is there
a better way?
--
lucas
-------------------------
Perl Coder since 2001
shift || die;
-------------------------
Jul 17 '05 #1
8 20604
nc
lucas wrote:

I have a text file that i would like to use PHP to read from. each line ends with a \n. i need to read from the beginning of the line, only upto that \n
right now i'm using this, which is sloppy and slow:

if ($chr = fgetc($file)) != "\n") { }

which of course reads in one byte at a time till the end of line. is there a better way?

Yes; read up on fgets():

http://www.php.net/fgets

Cheers,
NC

Jul 17 '05 #2
nc@iname.com wrote:
lucas wrote:
if ($chr = fgetc($file)) != "\n") { }
http://www.php.net/fgets


If you need to process all lines in the file (and the file isn't
extraordinarily big) also consider

http://www.php.net/file
http://www.php.net/file_get_contents (for PHP >= 4.3.0)

--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jul 17 '05 #3
nc@iname.com wrote:

Yes; read up on fgets():


Thanks. I read about that one before, but the manual says you need to
specify x amount of bytes, after you mentioned it, i checked it closer, and
the size was optional. thanks much. sped up my script from 12 secs
to .5 ;)
--
lucas
-------------------------
Perl Coder since 2001
shift || die;
-------------------------
Jul 17 '05 #4
you could also read the entire file into one string, then use explode()
and the \n delimiter. Its very low-level and childish, but works
effectivly and is easy to understand and work with..

Ex:
$filename = "yourfile.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);

$lines = explode("\n",$lines);
foreach($lines as $line){
// line is stored in $line
}

Jul 17 '05 #5
jblanch <jb*****@gmail.com> wrote:
$filename = "yourfile.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);

$lines = explode("\n",$lines);
foreach($lines as $line){
// line is stored in $line
}


Never heard of file()?
Jul 17 '05 #6
hm, guess file works too eh? but its the same idea, just without the
fopen/ect functions.

Jul 17 '05 #7
jblanch <jb*****@gmail.com> wrote:
hm, guess file works too eh? but its the same idea, just without the
fopen/ect functions.


file() works for any textfile, you example is only for unix type of
files. DOS type will have \r cluttering the end of lines. Mac files will
appear to contain 1 single line.

You could fix it by using preg_split("/(\r\n|\r|\n)/",$line) instead of
explode("\n",$line).

BTW is it really so hard to keep some context in your postings?

Jul 17 '05 #8

Daniel Tryba wrote:
jblanch <jb*****@gmail.com> wrote:
hm, guess file works too eh? but its the same idea, just without the fopen/ect functions.
file() works for any textfile, you example is only for unix type of
files. DOS type will have \r cluttering the end of lines. Mac files

will appear to contain 1 single line.

You could fix it by using preg_split("/(\r\n|\r|\n)/",$line) instead of explode("\n",$line).

BTW is it really so hard to keep some context in your postings?


I'm sorry, but correct me if i'm wrong, but didn't he state that he
needs to read to \n? yes, i'm sure he did. there is nothing wrong
with the method i described, and i'm 100% sure it would work for him.
Hows that for context?

Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by David Thomas | last post: by
3 posts views Thread by John Flynn | last post: by
4 posts views Thread by yo_mismo | last post: by
1 post views Thread by Magix | last post: by
35 posts views Thread by RyanS09 | last post: by
9 posts views Thread by Adi | last post: by
7 posts views Thread by bowlderster | last post: by
6 posts views Thread by Thomas Kowalski | last post: by
4 posts views Thread by Keith G Hicks | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.