5

In PHP I know currency codes (EUR, GBP, USD...), but I do not know locale. I need to get a currency symbol for them

GBP -> £
EUR -> €
USD -> $

Using

$obj = new \NumberFormatter( null, \NumberFormatter::CURRENCY);
echo $obj->formatCurrency( null, 'EUR');

I can get € 0.00, so the NumberFormatter library can convert currency code to currency symbol. But how to get currency symbol only?

$obj = new \NumberFormatter( null, \NumberFormatter::CURRENCY);
$obj->setTextAttribute ( \NumberFormatter::CURRENCY_CODE, 'EUR');
echo $obj->getSymbol ( \NumberFormatter::CURRENCY_SYMBOL);

Also do not do the translation and returns $ always.

Joe
  • 185
  • 1
  • 2
  • 11
  • if you want a propel solution, you need the local! [Check this answer: Get currency symbol in PHP](https://stackoverflow.com/a/30026774/5356216) – Zoltán Süle Feb 02 '18 at 09:32
  • Possible duplicate of [Get currency symbol in PHP](https://stackoverflow.com/questions/13897516/get-currency-symbol-in-php) – Brandon Kelly Feb 02 '18 at 14:22

2 Answers2

11

Unfortunately this isn’t as easy as it should be, but here’s how to get the currency symbol by currency code, for a locale:

function getCurrencySymbol($locale, $currency)
{
    // Create a NumberFormatter
    $formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);

    // Prevent any extra spaces, etc. in formatted currency
    $formatter->setPattern('¤');

    // Prevent significant digits (e.g. cents) in formatted currency
    $formatter->setAttribute(NumberFormatter::MAX_SIGNIFICANT_DIGITS, 0);

    // Get the formatted price for '0'
    $formattedPrice = $formatter->formatCurrency(0, $currency);

    // Strip out the zero digit to get the currency symbol
    $zero = $formatter->getSymbol(NumberFormatter::ZERO_DIGIT_SYMBOL);
    $currencySymbol = str_replace($zero, '', $formattedPrice);

    return $currencySymbol;
}

Tested with locales: ar, cs, da, de, en, en_GB, en_US, es, fr, fr_CA, he, it, ja, ko, nb, nl, ru, sk, sv, zh

Tested with currencies: AUD, BRL, CAD, CNY, EUR, GBP, HKD, ILS, INR, JPY, KRW, MXN, NZD, THB, TWD, USD, VND, XAF, XCD, XOF, XPF

Brandon Kelly
  • 2,033
  • 2
  • 21
  • 38
  • I think that the `$zero = $formatter->getSymbol(NumberFormatter::ZERO_DIGIT_SYMBOL);` isn't necessary and you can use `0` in the last operation `$currencySymbol = str_replace(0, '', $formattedPrice);` – Zoltán Süle Jan 31 '18 at 17:15
  • 2
    There is a way simpler solution. [Get currency symbol in PHP](https://stackoverflow.com/a/30026774/5356216) – Zoltán Süle Feb 02 '18 at 09:44
  • @ZoltánSüle way better! Thanks! – Brandon Kelly Feb 02 '18 at 14:21
  • Actually, this way works improperly for some cases (e.g. `getCurrencySymbol("cs", "CZK")`), leaving extra bytes after the string. I guess, it breaks UTF-8. – Denis V Oct 30 '19 at 21:05
-1

Possibly not the best solution but you could always do something like ...

$obj = new \NumberFormatter( 'en_us', \NumberFormatter::CURRENCY);
$formattedValue = $obj->formatCurrency( 0, 'EUR');
$currencySymbol = trim(str_replace('0.00','',$formattedValue));

The important thing is using a locale and value where you know the expected output format (e.g. en_us and 0.00) and you can then pick the currency symbol out correctly.

Note: this might need some adjustment for certain currencies

  • This will fail with currencies in India since they use a different 0 (०) plus a few other countries using other symbols. – shiznatix Aug 27 '15 at 07:39