Perform separation on a Data Line and mount an array

Asked

Viewed 63 times

1

I receive from a GPS a data line as in the first example below, and I have to treat it separating by comma in the same way as the second example:

  1. Example Original Data

     (027043011394BR00160422A2242.5954S04237.5592W000.40056470.000000000000L00000000)
    
  2. Example of how the exit has to be (SEPARATION BY COMMA)

     027043011394,BR00,160422,A,2242.5954S,04237.5592W,000.4,0056,70.0000,00000000L,00000000
    

And now Mount 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];
  • After placing the commas, just use the explode function to create the array with the data. The problem is to put the commas. Can you tell if this data is always the same size? I say, ID will always be 12 characters and so on?

  • That’s right Sizes don’t change

3 answers

2


You can use the function replace PHP, to break the original string. Ex:

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

And so get the first value, to catch 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 only take the rest of the string, and use zero always 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);
    ....
  • 1

    It worked Perfectly here for me thanks

2

There is not much secret, as the formatting of the data does not have a pattern the solution is to extract piece by piece via substring, by the intervals defined.

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

$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 initial position of each field and the second with the amount of characters to be extracted, in this second example the keys of the array will be numerical.

$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);

Exit:

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
)
  • In the need for another answer suggesting also "replace", it would be better to focus your efforts on solving open questions, than to replace the same solution, otherwise. Remembering that it is always possible to edit a response if you think that something is inadequate, or incomplete.

-2

You could use the function explodes.

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

View documentation: Explode function

Browser other questions tagged

You are not signed in. Login or sign up in order to post.