[英]Extract Latitude and Longitude from Google Map Link
Supposing I had the following link to a google map: https://www.google.com.gh/maps/place/Niagara+Falls/@43.0828162,-79.0763516,17z/data=!4m15!1m9!4m8!1m0!1m6!1m2!1s0x89d34307412d7ae9:0x29be1d1e689ce35b!2sNiagara+Falls,+NY+14303,+United+States!2m2!1d-79.0741629!2d43.0828162!3m4!1s0x89d34307412d7ae9:0x29be1d1e689ce35b!8m2!3d43.0828162!4d-79.0741629 假设我有以下指向Google地图的链接: https : //www.google.com.gh/maps/place/Niagara+Falls/@43.0828162,-79.0763516,17z/data=!4m15!1m9!4m8!1m0! 1m6!1m2!1s0x89d34307412d7ae9:0x29be1d1e689ce35b!2sNiagara + Falls + NY + 14303,+ United + States!2m2!1d-79.0741629!2d43.0828162!3m4!1s0x89d34307412e7ae9:0x29be1!4d
I would like to extract only the latitude and longitude from the following link. 我只想从以下链接中提取经度和纬度。 I assume I would need to use regular expression to do this but I have no idea how to go about it.
我认为我需要使用正则表达式来执行此操作,但是我不知道该如何处理。
The co-ordinates in this case would be 43.0828162,-79.0763516 在这种情况下,坐标为43.0828162,-79.0763516
I would use a simple RegX like this 我将使用像这样的简单RegX
preg_match('/@(\-?[0-9]+\.[0-9]+),(\-?[0-9]+\.[0-9]+)/', $url, $match );
You can see it here, using your url. 您可以使用您的网址在这里看到它。
https://regex101.com/r/hD7lV0/1 https://regex101.com/r/hD7lV0/1
just for sake of explaining it 只是为了解释它
@
Literal At sign one time @
Literal At Sign -
, literal hyphen/minus ?
-
,文字连字符/减号?
one or none times [0-9]
, +
one or more times [0-9]
, +
1次或多次 \\.
\\.
literal .
.
decimal one time [0-9]
, +
one or more times [0-9]
, +
1次或多次 ,
, literal ,
comma one time etc... ,
文字,
逗号等... I would do. 我会做。
<?php
$latLong = split(',',preg_match('/@([0-9\.-,]*?)\//',$url));
// $latLon = [lat, long] = $latLon[0],$latLom[1]
?>
If needs be you can test it with 如果需要,您可以使用
if (count($latLon) > 2){
die('Invalid URL Segment found.');
}
If you want to be really short with your regex 如果您想真正使用正则表达式做空
$latLong = split(',',preg_match('/@(.*?)\//',$url));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.