Let's start with simple piece of code to format money with NumberFormatter
:
$formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
echo $formatter->formatCurrency(123456789, 'JPY');
This prints: ¥123,456,789
.
This is ok if you want to format money.
But what I want to do is to get currency symbol (e.g. ¥) for given currency ISO 4217 code (e.g. JPY).
My first guess was to try using:
$formatter->getSymbol(NumberFormatter::CURRENCY_SYMBOL);
But that gives currency symbol for locale given in constructor (en_US), $ in my case.
Is there a way to get currency symbol by currency ISO 4217 code in PHP?
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');