And*_*sen 2 php google-maps trim explode
我想从中获得一些价值:
$bounds = '((-34.41859810454894, 150.5594567718506), (-34.375112955999064, 150.74124617004395))';
Run Code Online (Sandbox Code Playgroud)
我的目标是将四个数字中的每一个列为自己的变量:
$swla = -34.41859810454894;
$swlo = 150.5594567718506;
$nela = -34.375112955999064;
$nelo = 150.74124617004395;
Run Code Online (Sandbox Code Playgroud)
这是我糟糕的黑客尝试:
$pieces = explode("),", $bounds);
var_dump($pieces); //array(2) { [0]=> string(39) "((-34.41859810454894, 150.5594567718506" [1]=> string(43) " (-34.375112955999064, 150.74124617004395))" }
echo '<br />';
$sw = trim(substr($pieces[0], 2));
echo $sw .'<br />'; //-34.41859810454894, 150.5594567718506
$ne = trim(substr($pieces[1], 2), " ) ");
echo $ne.'<br />'; //-34.375112955999064, 150.74124617004395
$sw = explode(",", $sw);
var_dump($sw); //array(2) { [0]=> string(18) "-34.41859810454894" [1]=> string(18) " 150.5594567718506" }
echo '<br />';
$ne = explode(",", $ne); //array(2) { [0]=> string(19) "-34.375112955999064" [1]=> string(19) " 150.74124617004395" }
var_dump($ne);
echo '<br />';
Run Code Online (Sandbox Code Playgroud)
谢谢
最短的方法可能是preg_match_all:
preg_match_all('/[0-9.-]+/', $bounds, $matches);
list($swla, $swlo, $nela, $nelo) = $matches[0];
Run Code Online (Sandbox Code Playgroud)
如果你想要花车,那么你可以使用sscanf.但是,这些数字太精确,无法正确配合PHP双打,这样做会失去一些精确度.