我想从包含数字和字母的字符串中提取数字,如下所示:
"In My Cart : 11 items"我想提取数字11。
发布于 2017-10-09 03:54:46
其他方式(甚至是unicode字符串):
$res = array();
$str = 'test 1234 555 2.7 string ..... 2.2 3.3';
$str = preg_replace("/[^0-9\.]/", " ", $str);
$str = trim(preg_replace('/\s+/u', ' ', $str));
$arr = explode(' ', $str);
for ($i = 0; $i < count($arr); $i++) {
if (is_numeric($arr[$i])) {
$res[] = $arr[$i];
}
}
print_r($res); //Array ( [0] => 1234 [1] => 555 [2] => 2.7 [3] => 2.2 [4] => 3.3 ) https://stackoverflow.com/questions/6278296
复制相似问题