首页 > 解决方案 > How to convert a string number with text into numeric number

问题描述

I have a number like that : 342.50 EUR. How to table only the number : 342.50 I tried this, but it's not the result that I want

$str = '342.50 EUR';
preg_match_all('!\d+!', $str, $matches);
print_r($matches);

result : Array ( [0] => Array ( [0] => 342 [1] => 00 ) )

标签: php

解决方案


如果你对失去最后一个没问题,0那么你可以简单地使用floatval

$str = '342.50 EUR';

echo floatval($str); // 342.5

推荐阅读