Separate values from a variable

3

Hello friends I need to separate values from a variable I get values like this:

-4.08768, -63.141322 23/04/2017 22:00:00

I need the values separated each with a type variable like this:

-4.08768, -63.141322

23/04/2017

22:00:00

I need to send via post to my system but I am not able to separate someone from that force to separate this example:

<?php
$variavel = "-4.08768, -63.141322 23/04/2017 22:00:00";
echo $variavel;
?>
    
asked by anonymous 24.04.2017 / 04:56

3 answers

0
$variavel = "-4.08768, -63.141322 23/04/2017 22:00:00";

First we take the space after the comma and then make an explode with space so as not to separate the latitude of the longitude

$variavel=str_replace(", ",",",$variavel);

O explode

$partes = explode(' ',$variavel);

$coordenadas=$partes[0];
$data=$partes[1];
$hora=$partes[2];

And finally we put the space after the comma in the variable $ coordinates

$coordenadas=str_replace(",", ",",$coordenadas);

The output will be

echo $coordenadas; //-4.08768, -63.141322
echo $data; //23/04/2017
echo  $hora; //22:00:00
  

Putting it all together:

$variavel = "-4.08768, -63.141322 23/04/2017 22:00:00";
$variavel=str_replace(", ",",",$variavel);
$partes = explode(' ',$variavel);
$coordenadas=$partes[0];
$data=$partes[1];
$hora=$partes[2];
$coordenadas=str_replace(",", ",",$coordenadas);

echo $coordenadas;
echo $data;
echo  $hora;

example- ideone

  

The same result is obtained as follows:

$variavel = "-4.08768, -63.141322 23/04/2017 22:00:00";

$partes = explode(' ',$variavel);

$latitude=$partes[0]; //-4.08768,
$longitude=$partes[1]; //-63.141322
$data=$partes[2]; //23/04/2017
$hora=$partes[3]; // 22:00:00

$latLong=$latitude." ".$longitude; // -4.08768, -63.141322

example - ideone

    
24.04.2017 / 14:24
0

Use an explode to sort the way you need it eg:

$params = explode(',', $row['variavel']);
$lat = $params[0]; //-4.08768
$lon = $params[1]; //-63.141322 23/04/2017 22:00:00
$val = $params[2]; //0

$saida = $lon.','.$val.','.$lat;
    
24.04.2017 / 05:48
0

Using preg_split , you inform pattern regex so that the string is divided:

$variavel = "-4.08768, -63.141322 23/04/2017 22:00:00";

$split = preg_split("/\b\s/", $variavel);

var_dump($split);

In this way, it will return a array with three positions:

array(3) {
  [0]=>
  string(20) "-4.08768, -63.141322"
  [1]=>
  string(10) "23/04/2017"
  [2]=>
  string(8) "22:00:00"
}

See the example on ideone

    
24.04.2017 / 16:08