Read lines from text file using PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bonski
    New Member
    • Jun 2007
    • 53

    #16
    Originally posted by ajaxrand
    text.txt
    [CODE=text]
    Datalogs:
    Date: 2007/6/11
    Time: 13:9:3
    Device ID: Data Logger
    About total Numbers: 001891
    =============== ====
    =============== ======
    Record: 1
    Event Type: 3
    Year: 2007
    Month: 6
    Day: 7
    Hour: 5
    Minute: 9
    Second: 37.527237
    Latitude: 1.342335
    Longitude: 103.776921
    Altitude: 0.000000
    PDOP: 50.000000
    HDOP: 50.000000
    Satellite No: 3
    Speed(KMs/hr): 0.000000
    Direction: 0.000000
    =============== ======
    Record: 2
    Event Type: 1
    Year: 2007
    Month: 6
    Day: 7
    Hour: 5
    Minute: 9
    Second: 42.526936
    Latitude: 1.342367
    Longitude: 103.776861
    Altitude: 0.000000
    PDOP: 50.000000
    HDOP: 50.000000
    Satellite No: 3
    Speed(KMs/hr): 0.000000
    Direction: 0.000000
    =============== ======
    Record: 3
    Event Type: 1
    Year: 2007
    Month: 6
    Day: 7
    Hour: 5
    Minute: 9
    Second: 47.526634
    Latitude: 1.342375
    Longitude: 103.776858
    Altitude: 0.000000
    PDOP: 50.000000
    HDOP: 50.000000
    Satellite No: 3
    Speed(KMs/hr): 0.000000
    Direction: 0.000000
    =============== ======
    [/CODE]

    Try to modify this script as per your requirement.
    read_text.php
    [CODE=php]
    <?php
    $filename = "text.txt";
    $fd = fopen ($filename, "r");
    $contents = fread ($fd,filesize ($filename));
    fclose ($fd);

    $delimiter = "============== =======";
    $splitcontents = explode($delimi ter, $contents);

    foreach( $splitcontents as $record )
    {

    echo "$record<hr >";
    }

    ?>
    [/CODE]
    this is the same method your going to use with your new problem..

    take not of this..

    [PHP]$delimiter = "============== =======";
    $splitcontents = explode($delimi ter, $contents);[/PHP]

    this is how you extract it from text file...
    delimiter is the one that separates your data...
    in this format, the delimiter you going to extract is this $delimiter = "============== =======";
    Code:
    Record: 2
    Event Type: 1
    Year: 2007
    Month: 6
    Day: 7
    Hour: 5
    Minute: 9
    Second: 42.526936
    Latitude: 1.342367
    Longitude: 103.776861
    Altitude: 0.000000
    PDOP: 50.000000
    HDOP: 50.000000
    Satellite No: 3
    Speed(KMs/hr): 0.000000
    Direction: 0.000000
    =====================
    Record: 3
    Event Type: 1
    Year: 2007
    Month: 6
    Day: 7
    Hour: 5
    Minute: 9
    Second: 47.526634
    Latitude: 1.342375
    Longitude: 103.776858
    Altitude: 0.000000
    PDOP: 50.000000
    HDOP: 50.000000
    Satellite No: 3
    Speed(KMs/hr): 0.000000
    Direction: 0.000000
    results to this...

    // first data... inside an array
    Record: 2
    Event Type: 1
    Year: 2007
    Month: 6
    Day: 7
    Hour: 5
    Minute: 9
    Second: 42.526936
    Latitude: 1.342367
    Longitude: 103.776861
    Altitude: 0.000000
    PDOP: 50.000000
    HDOP: 50.000000
    Satellite No: 3
    Speed(KMs/hr): 0.000000
    Direction: 0.000000


    // 2nd data inside an array
    Record: 3
    Event Type: 1
    Year: 2007
    Month: 6
    Day: 7
    Hour: 5
    Minute: 9
    Second: 47.526634
    Latitude: 1.342375
    Longitude: 103.776858
    Altitude: 0.000000
    PDOP: 50.000000
    HDOP: 50.000000
    Satellite No: 3
    Speed(KMs/hr): 0.000000
    Direction: 0.000000


    now... to extract each data inside.. so that it will fit in your table with their corresponding fields..
    use delimiter $delimiter = "\n"; means new line..

    [PHP]$delimiter = "\n";
    $data_contents = explode($delimi ter, $splitcontents[$i]);[/PHP]

    //notice that $splitcontents[$i] is in array already.. 'cause your doing this with an array inside an array..

    Record: 2
    Event Type: 1
    Year: 2007
    Month: 6
    Day: 7
    Hour: 5
    Minute: 9
    Second: 42.526936
    Latitude: 1.342367
    Longitude: 103.776861
    Altitude: 0.000000
    PDOP: 50.000000
    HDOP: 50.000000
    Satellite No: 3
    Speed(KMs/hr): 0.000000
    Direction: 0.000000


    now the result is would line by line.. now use the same method.. but with a delimiter $delimiter = ':';

    remember.. its an array within an array... have fun.. ^___^

    Comment

    Working...