Perform separation on a Data Line and mount an array

1

I get a GPS from a data line as in the 1st example below, and I have to treat it by commas in the same way as the second example:

  • Example Data Originals

     (027043011394BR00160422A2242.5954S04237.5592W000.40056470.000000000000L00000000)
    
  • Example how it has to be the output (VIRGLE SEPARATION)

     027043011394,BR00,160422,A,2242.5954S,04237.5592W,000.4,0056,70.0000,00000000L,00000000
    
  • And now Assemble an array

    [0] => 027043011394 - ID (identificação do dispositivo) 
    [1] => BR00 - comando 
    [2] => 160422 - em DATE (o formato yymmdd) 
    [3] => A -? 
    [4] => 2242.5954S - o Latitude 
    [5] => 04237.5592W - Longitude 
    [6] => 000.4 - Speed (nnn.n format) 
    [7] => 0056 - Time a (o formato hhmmssas a UTC) 
    [8] => 470.0 - Designação / rolamento (?) 
    [9] => 00000000L - a Elevation 
    [0] => 00000000 -
    
    
    $ID                         = $parts[0];
    echo $comando               = $parts[1];
    echo $date                  = $parts[2];
    $A                          = $parts[3];
    $lat                        = $parts[4];
    $long                       = $parts[5];
    $speed                      = $parts[6];
    $time                       = $parts[7];
    $eleleva                    = $parts[8];
    $eleleva2                   = $parts[9];
    
        
    asked by anonymous 22.04.2016 / 03:45

    3 answers

    2

    You can use PHP's substr function to break the original String. Ex:

      $array = [];           
      $array[] = substr($stringOriginal,0,12);   //Ou atribui logo em $id
    

    And so get the first value, to get the next one, you can increment a variable, with the size taken from the string:

      $pt = 0;
      $array[] = substr($stringOriginal,$pt,12);
      $pt+=12;
      $array[] = substr($stringOriginal,$pt,4);
      $pt+=4;
      ....
    

    Or, if you prefer, you can just pick up the rest of the string, and always use zero in the second parameter.

        $array[] = substr($stringOriginal,0,12);
        $stringOriginal = substr($stringOriginal,12,strlen($stringOriginal)-12);
        $array[] = substr($stringOriginal,0,4);
        $stringOriginal = substr($stringOriginal,4,strlen($stringOriginal)-4);
        ....
    
        
    22.04.2016 / 04:29
    2

    It does not have much secret, as the formatting of the data does not have a standard, the solution is to extract pieces by piece via substring, at defined intervals.

    Use the substr() function to get the desired chunk of the string. The first argument is the string, the second is the start of 'capture' and the third is the number of characters. And finally use implode() to format the output as the values of the array separated by commas.

    $str = '027043011394BR00160422A2242.5954S04237.5592W000.40056470.000000000000L00000000';
    
    $info = array();
    $info['id'] = substr($str, 0, 12);
    $info['comando'] = substr($str, 12, 4);
    $info['date'] = substr($str, 16, 6);
    $info['A'] = substr($str, 22, 1);
    $info['lat'] = substr($str, 23, 10);
    $info['long'] = substr($str, 33, 11);
    $info['speed'] = substr($str, 44, 5);
    $info['time'] = substr($str, 49, 4);
    $info['designacao'] = substr($str, 53, 5);
    $info['eleleva'] = substr($str, 58, 12);
    $info['eleleva2'] = substr($str, 70, 8);
    
    echo implode(',', $info);
    

    Another solution would be to define two arrays, the first with the starting position of each field and the second with the amount of characters to be extracted, in this second examples the array keys will be numeric.

    $inicio = 0;
    $posicoes = [0, 12, 16, 22, 23, 33, 44, 49, 53, 58, 70];
    $caracteres = [12, 4, 6, 1, 10, 11, 5, 4, 5, 12, 8];
    $arr = [];
    
    $i = 0;
    while($i < 11){
        $arr[] = substr($str, $posicoes[$i], $caracteres[$i]);
        $i++;
    }
    
    
    echo "<pre>";
    print_r($arr);
    

    Output:

    Array
    (
        [0] => 027043011394
        [1] => BR00
        [2] => 160422
        [3] => A
        [4] => 2242.5954S
        [5] => 04237.5592W
        [6] => 000.4
        [7] => 0056
        [8] => 470.0
        [9] => 00000000000L
        [10] => 00000000
    )
    
        
    22.04.2016 / 19:27
    -2

    You could use the explode function.

    $var = explode(",", $texto);
    

    See the documentation: Explode Function

        
    22.04.2016 / 17:01