[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

Splitting files in to an array by 'end of line'.

Hi,

Need to split a file by the 'end of each line' in to an array.

Currently, if I have EOF at the end of each line I could do it like this:

$logfile = ("LOGS_TMP.TXT");
$handle = fopen($logfile, "r");
$contents = fread($handle, filesize($logfile));

$list = split ('EOF', $contents);

But I don't want to manually add EOF to every line.

Can't use:

$list = file( $logfile,'');

as the results that I am bringing in mess it up with 'Unterminated String Constant'.

Question 1 -

What can I put in place of EOF so that it splits by each line.

Question 2 -

If I can't do Q1, can I add EOF to the end of each file (Without saving it).

Cheers,
Jas.


0
jramsden
Asked:
jramsden
  • 3
1 Solution
 
Diablo84Commented:
You would use

\n

or if its in windows format

\r\n

and dont use split use explode, eg

explode("\n",$contents);
0
 
Diablo84Commented:
Also instead of fopen and fread you might want to just use file_get_contents
http://us2.php.net/manual/en/function.file-get-contents.php
0
 
jramsdenAuthor Commented:
This does work, but unfortunatly no with the file I'm bringing in.
It's a series of emails split by DATE, TIME, EMAIL, SUBJECT and BODY.
Because of the amount of text in the BODY section, it's not completing the task as specified.

Can you answer Q2 (Adding EOF to the end of each line).

Cheers,
Jason.
0
 
Diablo84Commented:
If you add EOF to the end of each line and then split by that i do not see that the results would be any different from just splitting by \n in the first place, if the body spans across multple lines then you may have to take a slightly different approach. Could i possibly see a sample entry from the file if this is the case.
0
 
TheUnkindCommented:
if you want to get it line by line use

$list=file($string);
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now