什么是快速清理货币字符串的方法

Rya*_*yan 5 php regex

可能重复:
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)

She*_*hef 5

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