SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: Text file Woes

  1. #1
    SitePoint Addict The Mog's Avatar
    Join Date
    Dec 2002
    Location
    Manchester UK
    Posts
    310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Text file Woes

    Hi Guys,

    i have a huge problem...........

    basically i am geting GPS data on my server in the following format, The data is saved in a text file,

    Code:
    $KENNY1,run,c7, 4680,,839527
    $GPRMC,102503.000,A,2640.1877,S,02756.0048,E,0.1,077.1,080607,,,A*7C
    
    $KENNY1,run,b,, 4680,,958585
    $GPRMC,102620.000,A,2640.1874,S,02756.0045,E,0.1,104.9,080607,,,A*7D
    
    $KENNY1,run,h9, 4680,,839527
    $GPRMC,102735.000,A,2640.1850,S,02756.0038,E,0.5,239.9,080607,,,A*7D
    
    $KENNY1,run,h5, 4680,,958585
    $GPRMC,102853.000,A,2640.1789,S,02756.0004,E,0.5,245.3,080607,,,A*77
    Above is EXACTLY how the file.txt looks

    Ok, now for the problem,

    I need to extract the data from the file and insert it into my DB,

    All that is important on the first line is the last number ie:958585 or 839527 (2 different units)

    then there are a few fields needed on the second line, But i need to know the number of the unit from the first line so i can associate the second line to the first,

    After extracting the data, and inserting it into my DB i would like to delete the line,

    Does anyone have any ideas on how i could accomplish this?

    Thanks

    Kenny

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I think you can probably do this:

    Split it into units (each unit is two lines) by splitting at the blank line (a double newline):
    PHP Code:
    $units explode("\n\n"filegetcontents('file.txt'));
    foreach (
    $units as $unit) {
      
    $lines explode("\n"$unit); 

      
    $firstlinebits explode(','$lines[0]);
      
    $id array_pop($firstlinebits); // last item on first line

      
    $secondlinebits explode(','$lines[1]);
      
    // get the data you need from the second line now, e.g.:
      
    $var1 $secondlinebits[4];
      
    $var2 $secondlinebits[7];

      
    //insert into DB using $id from first line and $varX from second line



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •