0

I have got the following string

http://www.domain.com/map.aspx?isTrafficAlert=true&lat=51.529900&lon=-0.785384

I need to extract lat and lon from this string.

I got lat=51.529900 and lon=-0.785384 using the following code

$str = "http://www.domain.com/map.aspx?isTrafficAlert=true&lat=51.529900&lon=-0.785384";
list(,$lat,$lon) = explode("&",$str);

But then can't get the number.

Any help is highly appreciated. Thanks in advance.

Prithviraj Mitra
  • 9,394
  • 12
  • 51
  • 87

3 Answers3

5

You can use parse_str along with parse_url like as

parse_str(parse_url($str, PHP_URL_QUERY), $latlon);
echo $latlon['lat'];
echo $latlon['lon'];
Federkun
  • 33,973
  • 8
  • 70
  • 82
Narendrasingh Sisodia
  • 20,667
  • 5
  • 43
  • 53
0
$str = "http://www.domain.com/map.aspx?isTrafficAlert=true&lat=51.529900&lon=-0.785384";

$var = parse_str($str, $array);

$lat = $array['lat'];
$lon = $array['lon'];
Ifedi Okonkwo
  • 3,108
  • 4
  • 25
  • 44
-2

Try this:

$str = "http://www.domain.com/map.aspx?isTrafficAlert=true&lat=51.529900&lon=-0.785384";

parse_str($str,$params);
print_r($params);

LAT: $params['lat']; LON: $params['lon'];

Hope it help;