从php中的字符串中提取正负浮点数

对于PHP我必须从字符串中提取浮点数.我是regex的新手,但在大多数情况下我找到了一个我有用的解决方案:

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);

唯一的问题是负值;有人可以改变我的表达方式,负数也包括在内吗?

谢谢!

解决方法:

-?\d+(?:\.\d+)?

这应该为你做.

$re = "/-?\\d+(?:\\.\\d+)?/m"; 
$str = "4.321 -3.1212"; 
$subst = "coach"; 

$result = preg_replace($re, $subst, $str);

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...