preg_match url get parameter parsing

戏子无情 提交于 2019-12-11 22:14:45

问题


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

$url = '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.


回答1:


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"
}



回答2:


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


来源:https://stackoverflow.com/questions/3530361/preg-match-url-get-parameter-parsing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!