在PHP中,我有一个包含lat/lng的字符串,例如:(38.889373, -77.00279599999999)
我想提取第一个数字(纬度),返回38.889373
.另外,我想提取第二个数字(lng),返回-77.00279599999999
.
正则表达式每次都会踢我的屁股。感谢帮助!
$string = '(38.889373, -77.00279599999999)';
// $string = str_replace('(','',$string);
// $string = str_replace(')','',$string);
$string = str_replace(array(')','('),'',$string);
$string = explode(',',$string);
for($i = 0; $i < count($string); $i++) {
echo $string[$i];
}
尽管每个人都有建议,但我真的认为正则表达式是最简单的方法。 我相信我们可以假设每个字符串/行只有两个数字(纬度和对数)。
preg_match_all("/['d.-]+/", $str, $matches);
对数组(如果有多个纬度/纬度集)在 $matches[0]
中。 然后,您可以使用array_chunk($matches[0], 2)
.