我应该使用爆炸、分割和/或修剪吗?又如何呢?
我想从中得到一些值:
$bounds = '((-34.41859810454894, 150.5594567718506), (-34.375112955999064, 150.74124617004395))';
我的目标是将四个数字中的每一个都列为自己的变量:
$swla = -34.41859810454894;
$swlo = 150.5594567718506;
$nela = -34.375112955999064;
$nelo = 150.74124617004395;
这是我蹩脚的黑客尝试:
$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 />';
谢谢
I'd like to get some values out of this:
$bounds = '((-34.41859810454894, 150.5594567718506), (-34.375112955999064, 150.74124617004395))';
My goal is to list each of the four numbers as its own variable:
$swla = -34.41859810454894;
$swlo = 150.5594567718506;
$nela = -34.375112955999064;
$nelo = 150.74124617004395;
Here's my crappy hack attempt:
$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 />';
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
最短的方法可能是
preg_match_all
:如果你想要浮动,那么你可以使用
sscanf
。然而,这些数字太精确,无法正确地适合 PHP 双精度数,这样做会损失一些精度。The shortest method is probably
preg_match_all
:If you want floats, then you can use
sscanf
. However, these numbers are too precise to fit correctly in PHP doubles, you will lose some precision in doing so.只是为了表明您可以使用 sscanf()
给出
Just to show you can use sscanf()
gives
您可以使用
sscanf()
< /strong>:这会将您的坐标转换为
浮点
。您可以在此处了解有关格式的更多信息。You can use
sscanf()
:That will convert your coordinates to
floats
. You can learn more about formats here.就我个人而言,我会这样做:-
输出:-
我不知道你需要多少精度,但这种方法确实会损失一些。
Personally I would have done this:-
Output:-
I don't know how much precision you need, but this method does lose some.
我喜欢 preg_match 版本,但根据您的使用情况,您当然也可以使用explode 将它们加载到数组中。
然后,您可以将 LatLong 数组推出到带有变量名称等的关联数组中。这实际上只是您是否要使用正则表达式或输入的模板化程度的问题。
I like the preg_match version, but depending on your usage you can certainly use explode to load these into arrays as well.
You could then either push out LatLong array into an associative array with your variable names/etc. It's really just a matter of whether or not you want to use regular expressions or how strongly templated your inputs are.