[PHP] Reading Text file by line

2003-02-24 Thread Anthony
I need to read a text file by line, if I open the file and use fgets() then
it doesn't pick up the EOL corretly.  So I was planning on reading the whole
file in with fread() and then breaking it up by EOL characters.  My question
is, how do I do it?  What character do I search for in the string?  Anyone
have a simple example on how to do this?  Thanks.



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Reading Text file by line

2003-02-24 Thread M.A.Bond
Using 

$var=file('filename.txt');

Creates an array, with each element in the array being 1 line of the file.

Thanks

Mark


-Original Message-
From: Anthony [mailto:[EMAIL PROTECTED] 
Sent: 24 February 2003 16:24
To: [EMAIL PROTECTED]
Subject: [PHP] Reading Text file by line


I need to read a text file by line, if I open the file and use fgets() then
it doesn't pick up the EOL corretly.  So I was planning on reading the whole
file in with fread() and then breaking it up by EOL characters.  My question
is, how do I do it?  What character do I search for in the string?  Anyone
have a simple example on how to do this?  Thanks.



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Reading Text file by line

2003-02-24 Thread Ernest E Vogelsinger
At 17:24 24.02.2003, Anthony spoke out and said:
[snip]
I need to read a text file by line, if I open the file and use fgets() then
it doesn't pick up the EOL corretly.  So I was planning on reading the whole
file in with fread() and then breaking it up by EOL characters.  My question
is, how do I do it?  What character do I search for in the string?  Anyone
have a simple example on how to do this?  Thanks.
[snip] 

A line is usually terminated by either LF (Unix-Style) or CRLF (Win
style). I heard that some macies do it the other way round, terminating
with either only CR ot LFCR.

If you don't know exactly how lines will be terminated, first use
str_replace to normalize the line terminations:

$data = str_replace(array(\r\n, \n\r, \r, \n), \n, $data);

then simple explode() the data to receive an array of lines:

$arlines = explode(\n, $data);

You could also use preg_split to combine these operations:

$arlines = preg_split(/(\r\n|\n\r|\r|\n)/s, $data);

Disclaimer: all untested.

-- 
   O Ernest E. Vogelsinger 
   (\) ICQ #13394035 
^ http://www.vogelsinger.at/


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php