473,396 Members | 2,121 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 20680
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: David Thomas | last post by:
Hi there, a while ago, I posted a question regarding reading japanese text from a text file. Well, since I solved the problem, I thought I'd post my solution for the benefit of other people with...
6
by: Ruben | last post by:
Hello. I am trying to read a small text file using the readline statement. I can only read the first 2 records from the file. It stops at the blank lines or at lines with only spaces. I have a...
3
by: John Flynn | last post by:
hi, having problems reading from and writing back to the same file. basically, i want to read lines of text from a file and reverse them and write them back to the same file.. it has to...
4
by: yo_mismo | last post by:
Hi everyone, I'm trying to read the first line of a file this way: .... .... .... .... new_line=0; while((read=read(fd, &info, sizeof(info))) > 0 && !new_line){ if (strcmp(&info, "\n") !=...
1
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each...
35
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt",...
9
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still...
7
by: bowlderster | last post by:
Hello, all. This is the text file named test.txt. 1041 1467 7334 9500 2169 7724 3478 3358 9962 7464 6705 2145 6281 8827 1961 1491 3995 3942 5827 6436 6391 6604 4902 1153 1292 4382 9421 1716...
6
by: Thomas Kowalski | last post by:
Hi, currently I am reading a huge (about 10-100 MB) text-file line by line using fstreams and getline. I wonder whether there is a faster way to read a file line by line (with std::string line)....
4
by: Keith G Hicks | last post by:
I'm trying to read a text file and alter the contents of specific lines in the file. I know how to use streamreader to read each line of a file. I'm doing that already to get the data into a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.