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
  • 11,002
  • 13
  • 58
  • 99

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
  • 36,084
  • 8
  • 78
  • 90
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
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,406
  • 4
  • 33
  • 45
-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;