Get currency symbol in PHP

前端 未结 8 819
春和景丽
春和景丽 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 01:08

    Since the symbols can be multi-byte I used mb_*() functions to correctly grab the all non-punctuation and non-digit chars which would just leaves the symbol.

    function get_currency_symbol($string)
    {
        $symbol = '';
        $length = mb_strlen($string, 'utf-8');
        for ($i = 0; $i < $length; $i++)
        {
            $char = mb_substr($string, $i, 1, 'utf-8');
            if (!ctype_digit($char) && !ctype_punct($char))
                $symbol .= $char;
        }
        return $symbol;
    }
    
    $format = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
    $string = $format->formatCurrency(123456789, 'JPY');
    echo get_currency_symbol($string);
    
    0 讨论(0)
  • 2020-12-14 01:10

    Try this variant:

    $formatter->getSymbol(NumberFormatter::INTL_CURRENCY_SYMBOL);
    
    0 讨论(0)
提交回复
热议问题