我应该使用爆炸、分割和/或修剪吗?又如何呢?

发布于 2024-12-21 23:32:56 字数 1119 浏览 0 评论 0原文

我想从中得到一些值:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

捶死心动 2024-12-28 23:32:56

最短的方法可能是 preg_match_all

preg_match_all('/[0-9.-]+/', $bounds, $matches);
list($swla, $swlo, $nela, $nelo) = $matches[0];

如果你想要浮动,那么你可以使用 sscanf。然而,这些数字太精确,无法正确地适合 PHP 双精度数,这样做会损失一些精度。

The shortest method is probably preg_match_all:

preg_match_all('/[0-9.-]+/', $bounds, $matches);
list($swla, $swlo, $nela, $nelo) = $matches[0];

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.

半衬遮猫 2024-12-28 23:32:56

只是为了表明您可以使用 sscanf()

$bounds = '((-34.41859810454894, 150.5594567718506), (-34.375112955999064, 150.74124617004395))';

sscanf($bounds,'((%[-0-9.], %[-0-9.]), (%[-0-9.], %[-0-9.]))',$swla,$swlo,$nela,$nelo);

echo $swla,PHP_EOL;
echo $swlo,PHP_EOL;
echo $nela,PHP_EOL;
echo $nelo,PHP_EOL;

给出

-34.41859810454894 
150.5594567718506 
-34.375112955999064 
150.74124617004395

Just to show you can use sscanf()

$bounds = '((-34.41859810454894, 150.5594567718506), (-34.375112955999064, 150.74124617004395))';

sscanf($bounds,'((%[-0-9.], %[-0-9.]), (%[-0-9.], %[-0-9.]))',$swla,$swlo,$nela,$nelo);

echo $swla,PHP_EOL;
echo $swlo,PHP_EOL;
echo $nela,PHP_EOL;
echo $nelo,PHP_EOL;

gives

-34.41859810454894 
150.5594567718506 
-34.375112955999064 
150.74124617004395
夏尔 2024-12-28 23:32:56

您可以使用sscanf()< /strong>:

sscanf($bounds, "((%f, %f), (%f, %f))", $swla, $swlo, $nela, $nelo);

这会将您的坐标转换为浮点。您可以在此处了解有关格式的更多信息。

You can use sscanf():

sscanf($bounds, "((%f, %f), (%f, %f))", $swla, $swlo, $nela, $nelo);

That will convert your coordinates to floats. You can learn more about formats here.

霞映澄塘 2024-12-28 23:32:56

就我个人而言,我会这样做:-

$bounds = '((-34.41859810454894, 150.5594567718506), (-34.375112955999064, 150.74124617004395))';
$bounds = str_ireplace(array('(', ')'), '', $bounds);
$bounds = explode(',', $bounds);
foreach($bounds as $key => $bound){
    $bounds[$key] = floatval($bound);
}
var_dump($bounds);

输出:-

数组
0 =>浮动-34.418598104549
1 =>浮动150.55945677185
2=>浮动-34.375112955999
3=>浮动150.74124617004

我不知道你需要多少精度,但这种方法确实会损失一些。

Personally I would have done this:-

$bounds = '((-34.41859810454894, 150.5594567718506), (-34.375112955999064, 150.74124617004395))';
$bounds = str_ireplace(array('(', ')'), '', $bounds);
$bounds = explode(',', $bounds);
foreach($bounds as $key => $bound){
    $bounds[$key] = floatval($bound);
}
var_dump($bounds);

Output:-

array
0 => float -34.418598104549
1 => float 150.55945677185
2 => float -34.375112955999
3 => float 150.74124617004

I don't know how much precision you need, but this method does lose some.

笑咖 2024-12-28 23:32:56

我喜欢 preg_match 版本,但根据您的使用情况,您当然也可以使用explode 将它们加载到数组中。

 $bounds = '((-34.41859810454894, 150.5594567718506), (-34.375112955999064, 150.74124617004395))';
 $tempArray = explode(',', $bounds); 

 //Gives you $tempArray[0] = ((-34.41859810454894
 //and $tempArray[1] =  150.5594567718506)
 //and $tempArray[2] = (-34.375112955999064
 //and $tempArray[3] =  150.74124617004395))

 $latLongArray = array();
 foreach($tempArray as $latLong)
 {
     $tempValue = str_replace('(', '', $tempValue);
     $tempValue = str_replace(')', '', $tempValue);
     $latLongArray[] = trim($tempValue);
 }

然后,您可以将 LatLong 数组推出到带有变量名称等的关联数组中。这实际上只是您是否要使用正则表达式或输入的模板化程度的问题。

I like the preg_match version, but depending on your usage you can certainly use explode to load these into arrays as well.

 $bounds = '((-34.41859810454894, 150.5594567718506), (-34.375112955999064, 150.74124617004395))';
 $tempArray = explode(',', $bounds); 

 //Gives you $tempArray[0] = ((-34.41859810454894
 //and $tempArray[1] =  150.5594567718506)
 //and $tempArray[2] = (-34.375112955999064
 //and $tempArray[3] =  150.74124617004395))

 $latLongArray = array();
 foreach($tempArray as $latLong)
 {
     $tempValue = str_replace('(', '', $tempValue);
     $tempValue = str_replace(')', '', $tempValue);
     $latLongArray[] = trim($tempValue);
 }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文