Using PHP for parsing

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 "&lt;br&gt;";

}

}
fclose($file);
?>
----my output—
$GPRMC 040100.284 04 01 00.284 14460.28 V 190810 N47
$GPRMC 040111.000 04 01 11.000 14471.00 V 190810 N
49
$GPRMC 040123.000 04 01 23.000 14483.00 V 190810 N48
$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 A1F
$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 A6F
$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

Ah, I see.

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


<?php
error_reporting(-1);
ini_set('display_errors', true);

class Filter extends FilterIterator
{
  public function accept(){
    return in_array(
      substr($this->current(), 0, 6),
      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


$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 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.

Ok, given this code:-


<?php
error_reporting(-1);
ini_set('display_errors', true);

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

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

fclose($file);
?>

…we should get:-


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
error_reporting(-1);
ini_set('display_errors', true);

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

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

fclose($file);
?>

Any clearer Paul? :slight_smile:

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

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

All you need to do is use the php file open/read functions (see this tutorial) and [URL=“http://php.net/manual/en/function.explode.php”]explode() to extract the records you need from the raw data file.

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


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

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.