By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,510 Members | 1,821 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,510 IT Pros & Developers. It's quick & easy.

read line from text file

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
hm, guess file works too eh? but its the same idea, just without the
fopen/ect functions.

Jul 17 '05 #7

P: n/a
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

P: n/a

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.