开发者

preg_match url get parameter parsing

I am trying to extract the latitude and longitude of a google maps url. Such an url could look like this:

$u开发者_运维技巧rl = 'http://maps.google.com/?ie=UTF8&ll=39.811856,11.309322&spn=0,0.485802&t=h&z=12&layer=c&cbll=39.311856,11.519322&panoid=1hltSSOoqv5H1dVKZWFkaA&cbp=13,117.95,,1,16.71';

As you can see, there's several location sets in the url. The cbll variable seems to be the right one in my case.

This is what I came up with:

preg_match("~&cbll=(-?\d+\.?\d*),(-?\d+\.?\d*)~", $url, $matches);

The problem: The preg_match seems to match the first '&ll=' in the url, and not the cbll part. I get the "&ll=" part of the url as result.


It works for me, if I var_dump $matches, I see this

array(3) {
  [0]=>
  string(25) "&cbll=39.311856,11.519322"
  [1]=>
  string(9) "39.311856"
  [2]=>
  string(9) "11.519322"
}


parse_str(parse_url($url, PHP_URL_QUERY), $vars);
list($a, $b) = explode(",", $vars['cbll']);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜