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;
------------------------- 8 20680
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 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;
-------------------------
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
}
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()?
hm, guess file works too eh? but its the same idea, just without the
fopen/ect functions.
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?
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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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") !=...
|
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...
|
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",...
|
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...
|
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...
|
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)....
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |