Get currency symbol in PHP

First of all, there is no international global currency symbol table, that anyone on the planet could read and understand.

In each region/country the currency symbols will differ, that`s why you must determine them based on who is reading, using the browser / user locale.

The correct way is as you guessed, using NumberFormatter::CURRENCY_SYMBOL, but you first have to set the appropriate locale like en-US@currency=JPY:

$locale='en-US'; //browser or user locale
$currency='JPY';
$fmt = new NumberFormatter( $locale."@currency=$currency", NumberFormatter::CURRENCY );
$symbol = $fmt->getSymbol(NumberFormatter::CURRENCY_SYMBOL);
header("Content-Type: text/html; charset=UTF-8;");
echo $symbol;

This way the symbol will be understandable by the user.

For example, $symbol will be:

  • Canadian dollar (CAD) : CA$ in USA, CAD in Romania , $CA in Iran
  • Iran Rial (IRR): IRR in USA, while in Iran will be

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

If you set the locale using this function setlocale("LC_ALL", "es_AR"); You can use localeconv()['currency_symbol'] or localeconv()['int_curr_symbol'] to get the locale currency symbol and the international variation of the currency symbol.


I achieved this using https://github.com/symfony/Intl:

Symfony\Component\Intl\Intl::getCurrencyBundle()->getCurrencySymbol('EUR')

returns

'€'.

Symfony version > 4.3

It's worth pointing out for SF4.3 and above this has been deprecated:

/**
 * Returns the bundle containing currency information.
 *
 * @return CurrencyBundleInterface The currency resource bundle
 *
 * @deprecated since Symfony 4.3, to be removed in 5.0. Use {@see Currencies} instead.
 */
public static function getCurrencyBundle(): CurrencyBundleInterface
{

So, instead you can do:

use Symfony\Component\Intl\Currencies;
echo Currencies::getSymbol('AUD');