SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 29
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face Using PHP for parsing

    Dear all,

    I have a text file in notepad which is log downloaded from a mobile application. There are two important data I wish to extract and separate from the log files. Could someone please give me a clue on how php can help parse this log files so i can separate the various data into a table? I have attached a sample file for your look up.
    The two data that need separated is the $GPGGA and $GPRMC. The content of the file in reality is about 300kb but had to reduce it. So it is explanaratory purposes. I have done similar thing in excel and VB script but excel is slow and not able to take data of more than 30 thousand rows.

    Thanks for your assistance in advance.

    Paul_paisley
    Attached Files Attached Files

  2. #2
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this the same issue as in your other thread?

    If so, you can use explode() to separate out the data fields in each line.

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Hi Paul,

    Can you post an example of how you would like the data formatted, and how that relates to the data provided please?

    sample.txt
    Code:
    07:00:57
    IMEI 358998018395510
    $GPRMC,040100.284,V,,,,,,,190810,,,N*47
    CurCell 273953 LAC 29120 Name elisa MCC 244 MNC 05 MODE 6 SSI 86
    07:01:09
    IMEI 358998018395510
    $GPRMC,040111.000,V,,,,,,,190810,,,N*49
    $GPGGA,040108.000,,,,,0,,,,,,,,*75
    CurCell 273953 LAC 29120 Name elisa MCC 244 MNC 05 MODE 6 SSI 86
    07:01:21
    IMEI 358998018395510
    $GPRMC,040123.000,V,,,,,,,190810,,,N*48
    $GPGGA,040120.000,,,,,0,,,,,,,,*7F
    CurCell 273953 LAC 29120 Name elisa MCC 244 MNC 05 MODE 6 SSI 86
    07:01:33
    IMEI 358998018395510
    $GPRMC,040135.000,V,,,,,,,190810,,,N*4F
    $GPGGA,040132.000,,,,,0,,,,,,,,*7C
    CurCell 273953 LAC 29120 Name elisa MCC 244 MNC 05 MODE 6 SSI 92
    07:01:39
    IMEI 358998018395510
    $GPRMC,040142.011,A,6009.33584,N,02444.63728,E,1.8,325.0,190810,6.9,W,A*1F
    $GPGGA,040137.000,,,,,0,,,,,,,,*79
    CurCell 273953 LAC 29120 Name elisa MCC 244 MNC 05 MODE 6 SSI 92
    07:01:52
    IMEI 358998018395510
    $GPRMC,040154.000,A,6009.33325,N,02444.63796,E,,,190810,,,A*6B
    $GPGGA,040151.000,6009.33323,N,02444.63852,E,1,04,1.6,-6.6,M,35.1,M,,*75
    CurCell 273953 LAC 29120 Name elisa MCC 244 MNC 05 MODE 6 SSI 89
    07:02:04
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks and sorry for late reply. It is the same issue with the other thread. I have been struggling with this issue and am glad you have come to my aid.

  5. #5
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    Hi Paul,

    Can you post an example of how you would like the data formatted, and how that relates to the data provided please?

    sample.txt
    Code:
    07:00:57
    IMEI 358998018395510
    $GPRMC,040100.284,V,,,,,,,190810,,,N*47
    CurCell 273953 LAC 29120 Name elisa MCC 244 MNC 05 MODE 6 SSI 86
    07:01:09
    IMEI 358998018395510
    $GPRMC,040111.000,V,,,,,,,190810,,,N*49
    $GPGGA,040108.000,,,,,0,,,,,,,,*75
    CurCell 273953 LAC 29120 Name elisa MCC 244 MNC 05 MODE 6 SSI 86
    07:01:21
    IMEI 358998018395510
    $GPRMC,040123.000,V,,,,,,,190810,,,N*48
    $GPGGA,040120.000,,,,,0,,,,,,,,*7F
    CurCell 273953 LAC 29120 Name elisa MCC 244 MNC 05 MODE 6 SSI 86
    07:01:33
    IMEI 358998018395510
    $GPRMC,040135.000,V,,,,,,,190810,,,N*4F
    $GPGGA,040132.000,,,,,0,,,,,,,,*7C
    CurCell 273953 LAC 29120 Name elisa MCC 244 MNC 05 MODE 6 SSI 92
    07:01:39
    IMEI 358998018395510
    $GPRMC,040142.011,A,6009.33584,N,02444.63728,E,1.8,325.0,190810,6.9,W,A*1F
    $GPGGA,040137.000,,,,,0,,,,,,,,*79
    CurCell 273953 LAC 29120 Name elisa MCC 244 MNC 05 MODE 6 SSI 92
    07:01:52
    IMEI 358998018395510
    $GPRMC,040154.000,A,6009.33325,N,02444.63796,E,,,190810,,,A*6B
    $GPGGA,040151.000,6009.33323,N,02444.63852,E,1,04,1.6,-6.6,M,35.1,M,,*75
    CurCell 273953 LAC 29120 Name elisa MCC 244 MNC 05 MODE 6 SSI 89
    07:02:04
    Below is an example of how the data is formated:

    $GPRMC,040142,A,6009.3358,N,02444.6373,E,1.62,325,190810,,
    $GPRMC,040154,A,6009.3333,N,02444.6380,E,0.00,0,190810,,
    $GPRMC,040206,A,6009.3166,N,02444.6459,E,0.00,0,190810,,

    The entries in the raw text file begining with $GPRMC and $GPGA are the ones am interested in i.e the highligted ones. So I want to separate the $GPRMC from the $GPGGA enteries from the raw text file. If I can separate them, then I can move on to compare the two entries which would now be in separate text files with same entries in another text file but whose data collection mode is different. The relationship is that the format given is an example of one of the entries collected with one data collection mode while the raw text file contain two entries collected with 2 data collection mode. If you don't understand me please ask me to clarify more.

    Thanks for you assistance.

    paul

  6. #6
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All you need to do is use the php file open/read functions (see this tutorial) and explode() to extract the records you need from the raw data file.

  7. #7
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kalon View Post
    All you need to do is use the php file open/read functions (see this tutorial) and explode() to extract the records you need from the raw data file.
    Many thanks Kalon. I would look up the tutorial and see what I can do. I would get back to you on what I come up. Thanks once again.

    Cheers

  8. #8
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Just a quick pointer (no pun intended)
    PHP Code:
    <?php
    error_reporting
    (-1);
    ini_set('display_errors'true);

    $file fopen('sample.txt''r');

    for(
    $line 1false === feof($file); $line++){
      
      
    $entry fgets($file);
      
      if(
    in_array(substr($entry06), array('$GPGGA''$GPRMC'))){
        
    #the entry begins with $GPGGA or $GPRMC
      
    }
      
    }

    fclose($file);
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  9. #9
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    An alternative to Anthony's quick sample, which abstracts away manually opening and looping over the file (checking for EOF) and handily separates out the CSV fields into an array for each line, might look something like the following.

    PHP Code:
    <?php

    $file 
    = new SplFileObject('sample.txt');

    // Read the file as a CSV-formatted file
    $file->setFlags(SplFileObject::READ_CSV);

    // Scan over each line for the ones that we want
    foreach ($file as $line) {
        if (
    in_array($line[0], array('$GPGGA''$GPRMC'))) {
            
    // Do whatever you want here
            
    var_dump($line);
        }
    }
    P.S. It's nice to be back, I missed Sitepoint.
    Salathe
    Software Developer and PHP Manual Author.

  10. #10
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    ...and it's good to have you back.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  11. #11
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys am busy trying it out and having some errors. I would get back soon. Hope it works out.

  12. #12
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    Just a quick pointer (no pun intended)
    PHP Code:
    <?php
    error_reporting
    (-1);
    ini_set('display_errors'true);

    $file fopen('sample.txt''r');

    for(
    $line 1false === feof($file); $line++){
      
      
    $entry fgets($file);
      
      if(
    in_array(substr($entry06), array('$GPGGA''$GPRMC'))){
        
    #the entry begins with $GPGGA or $GPRMC
      
    }
      
    }

    fclose($file);
    ?>
    Hi Anthony this what I was able to put up with your suggestion first:
    <?php
    error_reporting(-1);
    ini_set('display_errors', true);

    $file = fopen('track20100819.txt', 'r');

    for($line = 1; false === feof($file); $line++){

    $entry = fgets($file);

    if(in_array(substr($entry, 0, 6), $outputstrings = array('$GPGGA', '$GPRMC'))){

    }

    }

    fclose($file);

    print_r ($outputstrings);
    ?>

    I have initialised the array which holds the text i want and tried to print the result. The displayed result is not useful. This is what it gives:
    Array ( [0] => $GPGGA [1] => $GPRMC )
    I thought it should print out the sorted entries. Please advise what am doing wrong. Thanks

    Paul

  13. #13
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Ok, given this code:-
    PHP Code:
    <?php
    error_reporting
    (-1);
    ini_set('display_errors'true);

    $file fopen('sample.txt''r');

    for(
    $line 1false === feof($file); $line++){
      
      
    $entry fgets($file);
      
      if(
    in_array(substr($entry06), array('$GPGGA''$GPRMC'))){
        
        echo 
    print_r(explode(','$entry), true), "\n";
        
      }
      
    }

    fclose($file);
    ?>
    ...we should get:-
    Code:
    Array
    (
        [0] => $GPRMC
        [1] => 040100.284
        [2] => V
        [3] => 
        [4] => 
        [5] => 
        [6] => 
        [7] => 
        [8] => 
        [9] => 190810
        [10] => 
        [11] => 
        [12] => N*47
    
    )
    
    Array
    (
        [0] => $GPRMC
        [1] => 040111.000
        [2] => V
        [3] => 
        [4] => 
        [5] => 
        [6] => 
        [7] => 
        [8] => 
        [9] => 190810
        [10] => 
        [11] => 
        [12] => N*49
    
    )
    
    Array
    (
        [0] => $GPGGA
        [1] => 040108.000
        [2] => 
        [3] => 
        [4] => 
        [5] => 
        [6] => 0
        [7] => 
        [8] => 
        [9] => 
        [10] => 
        [11] => 
        [12] => 
        [13] => 
        [14] => *75
    
    )
    
    Array
    (
        [0] => $GPRMC
        [1] => 040123.000
        [2] => V
        [3] => 
        [4] => 
        [5] => 
        [6] => 
        [7] => 
        [8] => 
        [9] => 190810
        [10] => 
        [11] => 
        [12] => N*48
    
    )
    
    Array
    (
        [0] => $GPGGA
        [1] => 040120.000
        [2] => 
        [3] => 
        [4] => 
        [5] => 
        [6] => 0
        [7] => 
        [8] => 
        [9] => 
        [10] => 
        [11] => 
        [12] => 
        [13] => 
        [14] => *7F
    
    )
    
    Array
    (
        [0] => $GPRMC
        [1] => 040135.000
        [2] => V
        [3] => 
        [4] => 
        [5] => 
        [6] => 
        [7] => 
        [8] => 
        [9] => 190810
        [10] => 
        [11] => 
        [12] => N*4F
    
    )
    
    Array
    (
        [0] => $GPGGA
        [1] => 040132.000
        [2] => 
        [3] => 
        [4] => 
        [5] => 
        [6] => 0
        [7] => 
        [8] => 
        [9] => 
        [10] => 
        [11] => 
        [12] => 
        [13] => 
        [14] => *7C
    
    )
    
    Array
    (
        [0] => $GPRMC
        [1] => 040142.011
        [2] => A
        [3] => 6009.33584
        [4] => N
        [5] => 02444.63728
        [6] => E
        [7] => 1.8
        [8] => 325.0
        [9] => 190810
        [10] => 6.9
        [11] => W
        [12] => A*1F
    
    )
    
    Array
    (
        [0] => $GPGGA
        [1] => 040137.000
        [2] => 
        [3] => 
        [4] => 
        [5] => 
        [6] => 0
        [7] => 
        [8] => 
        [9] => 
        [10] => 
        [11] => 
        [12] => 
        [13] => 
        [14] => *79
    
    )
    
    Array
    (
        [0] => $GPRMC
        [1] => 040154.000
        [2] => A
        [3] => 6009.33325
        [4] => N
        [5] => 02444.63796
        [6] => E
        [7] => 
        [8] => 
        [9] => 190810
        [10] => 
        [11] => 
        [12] => A*6B
    
    )
    
    Array
    (
        [0] => $GPGGA
        [1] => 040151.000
        [2] => 6009.33323
        [3] => N
        [4] => 02444.63852
        [5] => E
        [6] => 1
        [7] => 04
        [8] => 1.6
        [9] => -6.6
        [10] => M
        [11] => 35.1
        [12] => M
        [13] => 
        [14] => *75
    
    )
    In the above code sample, there are some bits to display the elements for demonstrations sake. If you just want the elements to work with, you'd be looking for something like...
    PHP Code:
    <?php
    error_reporting
    (-1);
    ini_set('display_errors'true);

    $file fopen('sample.txt''r');

    for(
    $line 1false === feof($file); $line++){
      
      
    $entry fgets($file);
      
      if(
    in_array(substr($entry06), array('$GPGGA''$GPRMC'))){
        
        
    $elements explode(',' $entry);
        
      }
      
    }

    fclose($file);
    ?>
    Any clearer Paul?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  14. #14
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    Ok, given this code:-
    PHP Code:
    <?php
    error_reporting
    (-1);
    ini_set('display_errors'true);

    $file fopen('sample.txt''r');

    for(
    $line 1false === feof($file); $line++){
      
      
    $entry fgets($file);
      
      if(
    in_array(substr($entry06), array('$GPGGA''$GPRMC'))){
        
        echo 
    print_r(explode(','$entry), true), "\n";
        
      }
      
    }

    fclose($file);
    ?>
    ...we should get:-
    Code:
    Array
    (
        [0] => $GPRMC
        [1] => 040100.284
        [2] => V
        [3] => 
        [4] => 
        [5] => 
        [6] => 
        [7] => 
        [8] => 
        [9] => 190810
        [10] => 
        [11] => 
        [12] => N*47
    
    )
    
    Array
    (
        [0] => $GPRMC
        [1] => 040111.000
        [2] => V
        [3] => 
        [4] => 
        [5] => 
        [6] => 
        [7] => 
        [8] => 
        [9] => 190810
        [10] => 
        [11] => 
        [12] => N*49
    
    )
    
    Array
    (
        [0] => $GPGGA
        [1] => 040108.000
        [2] => 
        [3] => 
        [4] => 
        [5] => 
        [6] => 0
        [7] => 
        [8] => 
        [9] => 
        [10] => 
        [11] => 
        [12] => 
        [13] => 
        [14] => *75
    
    )
    
    Array
    (
        [0] => $GPRMC
        [1] => 040123.000
        [2] => V
        [3] => 
        [4] => 
        [5] => 
        [6] => 
        [7] => 
        [8] => 
        [9] => 190810
        [10] => 
        [11] => 
        [12] => N*48
    
    )
    
    Array
    (
        [0] => $GPGGA
        [1] => 040120.000
        [2] => 
        [3] => 
        [4] => 
        [5] => 
        [6] => 0
        [7] => 
        [8] => 
        [9] => 
        [10] => 
        [11] => 
        [12] => 
        [13] => 
        [14] => *7F
    
    )
    
    Array
    (
        [0] => $GPRMC
        [1] => 040135.000
        [2] => V
        [3] => 
        [4] => 
        [5] => 
        [6] => 
        [7] => 
        [8] => 
        [9] => 190810
        [10] => 
        [11] => 
        [12] => N*4F
    
    )
    
    Array
    (
        [0] => $GPGGA
        [1] => 040132.000
        [2] => 
        [3] => 
        [4] => 
        [5] => 
        [6] => 0
        [7] => 
        [8] => 
        [9] => 
        [10] => 
        [11] => 
        [12] => 
        [13] => 
        [14] => *7C
    
    )
    
    Array
    (
        [0] => $GPRMC
        [1] => 040142.011
        [2] => A
        [3] => 6009.33584
        [4] => N
        [5] => 02444.63728
        [6] => E
        [7] => 1.8
        [8] => 325.0
        [9] => 190810
        [10] => 6.9
        [11] => W
        [12] => A*1F
    
    )
    
    Array
    (
        [0] => $GPGGA
        [1] => 040137.000
        [2] => 
        [3] => 
        [4] => 
        [5] => 
        [6] => 0
        [7] => 
        [8] => 
        [9] => 
        [10] => 
        [11] => 
        [12] => 
        [13] => 
        [14] => *79
    
    )
    
    Array
    (
        [0] => $GPRMC
        [1] => 040154.000
        [2] => A
        [3] => 6009.33325
        [4] => N
        [5] => 02444.63796
        [6] => E
        [7] => 
        [8] => 
        [9] => 190810
        [10] => 
        [11] => 
        [12] => A*6B
    
    )
    
    Array
    (
        [0] => $GPGGA
        [1] => 040151.000
        [2] => 6009.33323
        [3] => N
        [4] => 02444.63852
        [5] => E
        [6] => 1
        [7] => 04
        [8] => 1.6
        [9] => -6.6
        [10] => M
        [11] => 35.1
        [12] => M
        [13] => 
        [14] => *75
    
    )
    In the above code sample, there are some bits to display the elements for demonstrations sake. If you just want the elements to work with, you'd be looking for something like...
    PHP Code:
    <?php
    error_reporting
    (-1);
    ini_set('display_errors'true);

    $file fopen('sample.txt''r');

    for(
    $line 1false === feof($file); $line++){
      
      
    $entry fgets($file);
      
      if(
    in_array(substr($entry06), array('$GPGGA''$GPRMC'))){
        
        
    $elements explode(',' $entry);
        
      }
      
    }

    fclose($file);
    ?>
    Any clearer Paul?
    Thanks Anthony. It is a bit clear. I just want to display the element like this:
    $GPRMC, 040100.284,V etc in a single line if possible write it in another text file. I dont know if you get me right. I am pretty a novice here. sorry about that.

  15. #15
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Ah, I see.

    In which case, building upon Salathe's example, we can hide away some junk with...

    PHP Code:
    <?php
    error_reporting
    (-1);
    ini_set('display_errors'true);

    class 
    Filter extends FilterIterator
    {
      public function 
    accept(){
        return 
    in_array(
          
    substr($this->current(), 06),
          array(
            
    '$GPGGA',
            
    '$GPRMC'
          
    )
        );
      }
    }

    $source = new SplFileObject('sample.txt''r');
    $output = new SplFileObject('output.txt''w');

    foreach(new 
    Filter($source) as $line){
      
    $output->fwrite($line);
    }
    ?>
    So, output.txt now contains
    Code:
    $GPRMC,040100.284,V,,,,,,,190810,,,N*47
    $GPRMC,040111.000,V,,,,,,,190810,,,N*49
    $GPGGA,040108.000,,,,,0,,,,,,,,*75
    $GPRMC,040123.000,V,,,,,,,190810,,,N*48
    $GPGGA,040120.000,,,,,0,,,,,,,,*7F
    $GPRMC,040135.000,V,,,,,,,190810,,,N*4F
    $GPGGA,040132.000,,,,,0,,,,,,,,*7C
    $GPRMC,040142.011,A,6009.33584,N,02444.63728,E,1.8,325.0,190810,6.9,W,A*1F
    $GPGGA,040137.000,,,,,0,,,,,,,,*79
    $GPRMC,040154.000,A,6009.33325,N,02444.63796,E,,,190810,,,A*6B
    $GPGGA,040151.000,6009.33323,N,02444.63852,E,1,04,1.6,-6.6,M,35.1,M,,*75
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  16. #16
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    Ah, I see.

    In which case, building upon Salathe's example, we can hide away some junk with...

    PHP Code:
    <?php
    error_reporting
    (-1);
    ini_set('display_errors'true);

    class 
    Filter extends FilterIterator
    {
      public function 
    accept(){
        return 
    in_array(
          
    substr($this->current(), 06),
          array(
            
    '$GPGGA',
            
    '$GPRMC'
          
    )
        );
      }
    }

    $source = new SplFileObject('sample.txt''r');
    $output = new SplFileObject('output.txt''w');

    foreach(new 
    Filter($source) as $line){
      
    $output->fwrite($line);
    }
    ?>
    So, output.txt now contains
    Code:
    $GPRMC,040100.284,V,,,,,,,190810,,,N*47
    $GPRMC,040111.000,V,,,,,,,190810,,,N*49
    $GPGGA,040108.000,,,,,0,,,,,,,,*75
    $GPRMC,040123.000,V,,,,,,,190810,,,N*48
    $GPGGA,040120.000,,,,,0,,,,,,,,*7F
    $GPRMC,040135.000,V,,,,,,,190810,,,N*4F
    $GPGGA,040132.000,,,,,0,,,,,,,,*7C
    $GPRMC,040142.011,A,6009.33584,N,02444.63728,E,1.8,325.0,190810,6.9,W,A*1F
    $GPGGA,040137.000,,,,,0,,,,,,,,*79
    $GPRMC,040154.000,A,6009.33325,N,02444.63796,E,,,190810,,,A*6B
    $GPGGA,040151.000,6009.33323,N,02444.63852,E,1,04,1.6,-6.6,M,35.1,M,,*75
    Thanks for this latest reply. I would try that on out but first I like to say are a great guy Anthony. Salath, Kalon and guys on this forum are great and am happy to be on this forum. I am inspired and it is my desire to be good in PHP like you guys.

    Below is what I did earlier with your suggestion which i modified and the corresponding output whose result is satisfying:
    ---my code----
    <?php
    error_reporting(-1);
    ini_set('display_errors', true);

    $file = fopen('track20100819.txt', 'r');

    for($line = 1; false === feof($file); $line++){

    $entry = fgets($file);

    if(in_array(substr($entry, 0, 6), array('$GPRMC'))){

    $a = explode(",", $entry);
    print_r($a[0], false);
    echo "<br>";


    }

    }
    fclose($file);
    ?>
    ----my output---
    $GPRMC 040100.284 04 01 00.284 14460.28 V 190810 N*47
    $GPRMC 040111.000 04 01 11.000 14471.00 V 190810 N*49
    $GPRMC 040123.000 04 01 23.000 14483.00 V 190810 N*48
    $GPRMC 040135.000 04 01 35.000 14495.00 V 190810 N*4F
    $GPRMC 040142.011 04 01 42.011 14502.01 A 6009.33584 N 02444.63728 E 1.8 325.0 190810 19 08 10 6.9 W A*1F
    $GPRMC 040154.000 04 01 54.000 14514.00 A 6009.33325 N 02444.63796 E 190810 A*6B
    $GPRMC 040206.000 04 02 06.000 14526.00 A 6009.31657 N 02444.64591 E 190810 A*6F
    $GPRMC 040218.000 04 02 18.000 14538.00 A 6009.31536 N 02444.64979 E 1.3 244.6 190810 19 08 10 6.9 W A*1E
    ---
    I think am gaining from this forum. I appreciate.
    Now I would go on to try out your latest post which am sure would work out well too.

    My next step would be to be able to read in multiple text files so I can just use only one code to to extract. I would set to work on that and hope that you guy would have to help me if I come calling.

    Thanks to you all.

    Best Regars

    Paul

  17. #17
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want to process multiple input files and be flexible with your search criteria and not be limited to the first 6 chars. of each line, then this is one way of doing it.

    I haven't tested the code so hopefully there are no bugs, but you should be able to see the jist of what I am trying to do.

    Code PHP:
    <?php
    $searchCriteria = array('$GPGGA','$GPRMC');
    $inFiles = array('sample_1.txt','sample_2.txt','sample_3.txt','sample_4.txt',);
     
    //process each file in $inFiles
    foreach($inFiles as $inFileName) {
     //open the input and output files
     $inFile = fopen(inFileName,"r");
     $outFile = fopen(dirname($inFileName).basename($inFile,'.txt').'_out.txt',"w");
     //read the inFile line by line and output the line if searchCriteria is met
     while(!feof($inFile)) {
      $line = fgets($inFile);
         $lineTokens = explode(',',$line);
         if(in_array($lineTokens[0],$searchCriteria)) {
          fwrite($outFile,$line);
         }
     }
     
     //close the 2 files
     fclose($inFile);
     fclose($outFile);
    }
    ?>

  18. #18
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kalon View Post
    If you want to process multiple input files and be flexible with your search criteria and not be limited to the first 6 chars. of each line, then this is one way of doing it.

    I haven't tested the code so hopefully there are no bugs, but you should be able to see the jist of what I am trying to do.

    Code PHP:
    <?php
    $searchCriteria = array('$GPGGA','$GPRMC');
    $inFiles = array('sample_1.txt','sample_2.txt','sample_3.txt','sample_4.txt',);
     
    //process each file in $inFiles
    foreach($inFiles as $inFileName) {
     //open the input and output files
     $inFile = fopen(inFileName,"r");
     $outFile = fopen(dirname($inFileName).basename($inFile,'.txt').'_out.txt',"w");
     //read the inFile line by line and output the line if searchCriteria is met
     while(!feof($inFile)) {
      $line = fgets($inFile);
         $lineTokens = explode(',',$line);
         if(in_array($lineTokens[0],$searchCriteria)) {
          fwrite($outFile,$line);
         }
     }
     
     //close the 2 files
     fclose($inFile);
     fclose($outFile);
    }
    ?>
    Thanks Kalon. I would try the code and get back to you. Does this line fwrite($outFile,$line); mean I could write the output straight into another text file? Just to know, is there no way I can modify the previous code i showed in my last reply? maybe just add the lines that can handle the multiple text files? Hope to hear from you soon.

    Regards
    Paul

  19. #19
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_paisley View Post
    Does this line fwrite($outFile,$line); mean I could write the output straight into another text file?


    Yes - I think we are essentially doing the same thing in 2 different ways. The demo code I posted is the way I am used to handling files.

    I am looping through my $inFiles array and for each input file I am creating an output file. eg. for sample_1.txt I am creating an output file called sample_1_out.txt.

    Then I read one line at a time from sampl_1.txt and tokenizing it using explode(). If the first element in the $lineTokens array contains any of the elements in the $searchCriteria array I then write the whole current line read from sample_1.txt to sample_1_out.txt

    The foreach loop repeats the above for each file in the $inFiles array.

    Quote Originally Posted by paul_paisley View Post
    is there no way I can modify the previous code i showed in my last reply? maybe just add the lines that can handle the multiple text files?


    Your code should be modifiable to output the same thing as my code does using your OOP structure. Essentially you just need to wrap your code to handle 1 file with a foreach loop to handle mutiple files as in my demo.



  20. #20
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kalon View Post

    Yes - I think we are essentially doing the same thing in 2 different ways. The demo code I posted is the way I am used to handling files.

    I am looping through my $inFiles array and for each input file I am creating an output file. eg. for sample_1.txt I am creating an output file called sample_1_out.txt.

    Then I read one line at a time from sampl_1.txt and tokenizing it using explode(). If the first element in the $lineTokens array contains any of the elements in the $searchCriteria array I then write the whole current line read from sample_1.txt to sample_1_out.txt

    The foreach loop repeats the above for each file in the $inFiles array.



    Your code should be modifiable to output the same thing as my code does using your OOP structure. Essentially you just need to wrap your code to handle 1 file with a foreach loop to handle mutiple files as in my demo.

    Hello, i tried your suggestion and this line:
    $outFile = fopen(dirname($inFileName).basename($inFiles,'.txt').'_out.txt',"w");

    gave the following error:
    Warning: basename() expects parameter 1 to be string, resource given in C:\wamp\www\saterisk\index.php on line 14

    Warning: basename() expects parameter 1 to be string, resource given in C:\wamp\www\saterisk\index.php on line 14

    I am trying to figure it out. Any suggestions? Thanks.

    Paul

  21. #21
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    my mistake - sorry

    Code:
     
    $outFile = fopen(dirname($inFileName).basename($inFiles,'.txt').'_out.txt',"w");
    should be

    Code:
     
    $outFile = fopen(dirname($inFileName).basename($inFileName,'.txt').'_out.txt',"w");
    Hopefully there are no other errors.

    As I mentioned before, I haven't tested the code.

  22. #22
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there, the result is stunning. There are no errors now but the output is blank. What could be wrong is now my wonder. I dont know what you think. what do you think is wrong. Kalon?

  23. #23
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Hi Paul.

    What code are you using now? Please can make sure you pop it in [php] tags too, it hurts my eyes!
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  24. #24
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kalon View Post
    my mistake - sorry

    Code:
     
    $outFile = fopen(dirname($inFileName).basename($inFiles,'.txt').'_out.txt',"w");
    should be

    Code:
     
    $outFile = fopen(dirname($inFileName).basename($inFileName,'.txt').'_out.txt',"w");
    Hopefully there are no other errors.

    As I mentioned before, I haven't tested the code.
    sorry Kalon i was silly. i have the result in the file created. You are great. Many thanks. let me give you break for now. I remain greatful to this forum
    Regards

    Paul

  25. #25
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry about that Anthony. I moving on gradually and have passed the stage of handling multiple files with Kalon's suggestions. Thank you Tony


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
  •