在所有输入方案中格式化货币

时间:2013-05-21 05:17:15

标签: php regex

任何可以解决此问题的正则表达式专家?基本上,在所有不同的场景中需要相同的输出。只需要弄清楚正确的正则表达式。

<?php

 // Inputs
$numbers = array(
"1234",
"1 234,00",
"1234.00",
"1234,00",
"1.234,00",
"1.234.00",
"1,234.00",
"1,234,567.00",
"123,456.00",
"1.234.567,00",
"1 234 567,00",
"-1234",
    );

foreach ($numbers as $number) {
    echo $number . " => " . my_number_format($number) . " <br />";
}

// Currency format
function my_number_format($number){
    // return number_format((double)$number, 2, ',', ' ');
    $number = preg_replace('#(\d),(\d)#','$1$2',$number);

    setlocale(LC_MONETARY, 'sv_SE');
    return money_format('%!i', $number);
}
 ?>

0 个答案:

没有答案