Solved

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

Posted on 2004-10-27
183 Views
Last Modified: 2006-11-17
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
Question by:jramsden
    5 Comments
     
    LVL 27

    Expert Comment

    by:Diablo84
    You would use

    \n

    or if its in windows format

    \r\n

    and dont use split use explode, eg

    explode("\n",$contents);
    0
     
    LVL 27

    Expert Comment

    by:Diablo84
    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
     

    Author Comment

    by:jramsden
    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
     
    LVL 27

    Accepted Solution

    by:
    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
     
    LVL 2

    Expert Comment

    by:TheUnkind
    if you want to get it line by line use

    $list=file($string);
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Anonabox PRO Tor & VPN Router

    PRO is the most advanced way to fortify your privacy and online anonymity by layering the Tor network with VPN services. Use both together or separately, and without needing to download software onto your devices.

    Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
    The viewer will learn how to count occurrences of each item in an array.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now