Get currency symbol in PHP

前端 未结 8 832
春和景丽
春和景丽 2020-12-14 00:07

Let\'s start with simple piece of code to format money with NumberFormatter:

$formatter = new NumberFormatter(\'en_US\', NumberFormatter::CURREN         


        
8条回答
  •  南笙
    南笙 (楼主)
    2020-12-14 00:52

    Cryptic has a good answer, but there is a simpler way to do it:

    preg_replace('#[a-z0-9.]*#i', '', $formatter->formatCurrency('0', $currency))
    

    This is a nice simple inline solution that doesn't require declaring another function, however it also doesn't properly handle all cases - i.e. currencies where letters are part of the output. But for distinguishing between e.g. $ and £, it works fine.

提交回复
热议问题