将字符串转换为两个双精度数

时间:2014-03-27 08:38:33

标签: php

如果我有这样的字符串:'(1.23123,4.123123)'

我如何将其转换为两个双打?

$items = explode(',', $string);

$n1 = $items[0] // (1.23123

我的尝试:

floatval($n1) // 0
(double) $n1 // 0

我该如何转换?

3 个答案:

答案 0 :(得分:1)

你需要trim字符串周围的括号。通过将括号作为charlist传递,在trim内使用explode

$items = explode(',', trim($str,')('));

代码

<?php
$str='(1.23123, 4.123123)';
$items = explode(',', trim($str,')('));
$items=array_map('floatval',$items);
echo $n1 = $items[0]; // "prints" 1.23123
echo $n2 = $items[1]; // "prints" 4.123123

答案 1 :(得分:1)

您的数组

array_map('floatval', $array)

答案 2 :(得分:0)

试试此代码

   $string = "(1.23123, 4.123123)";
   preg_match_all("/[\d\.]+/", $string, $matches);
   print_r($matches[0]);
相关问题