可能重复:
PHP:unformat money
如何摆脱不是数字或点的一切,取而代之的,是.使用轻型正则表达式?
例子:
$50.45 = 50.45
USD 50.45 = 50.45
50,45 = 50.45
USD$ 50.45 = 50.45
Run Code Online (Sandbox Code Playgroud)
<?php
$money = array(
'$50.45',
'USD 50.45',
'50,45',
'USD$ 50.45'
);
// remove everything except a digit "0-9", a comma ",", and a dot "."
$money = preg_replace('/[^\d,\.]/', '', $money);
// replace the comma with a dot, in the number format ",12" or ",43"
$money = preg_replace('/,(\d{2})$/', '.$1', $money);
print_r($money);
?>
Run Code Online (Sandbox Code Playgroud)
输出:
Array
(
[0] => 50.45
[1] => 50.45
[2] => 50.45
[3] => 50.45
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3408 次 |
| 最近记录: |