【发布时间】:2015-06-25 19:32:02
【问题描述】:
对于 php,我必须从字符串中提取浮点数。我是正则表达式的新手,但我找到了一个在大多数情况下都适合我的解决方案:
Extract floating point numbers from a string in PHP
$str = '152.15 x 12.34 x 11mm';
preg_match_all('!\d+(?:\.\d+)?!', $str, $matches);
$floats = array_map('floatval', $matches[0]);
print_r($floats);
唯一的问题是负值;有人可以以正确包含负数的方式为我更改表达式吗?
谢谢!
【问题讨论】:
标签: php regex floating-point negative-number