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?
asked Dec 16, 2012 at 0:35
umpirskyumpirsky
9,75213 gold badges71 silver badges94 bronze badges
6
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 ﷼
answered May 4, 2015 at 9:30
idragosalexidragosalex
1,4751 gold badge12 silver badges8 bronze badges
1
I achieved this using //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'];
flumingo
5132 gold badges7 silver badges24 bronze badges
answered Jun 5, 2013 at 14:10
umpirskyumpirsky
9,75213 gold badges71 silver badges94 bronze badges
4
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];
answered Dec 16, 2012 at 1:16
cryptic ツcryptic ツ
15k9 gold badges53 silver badges80 bronze badges
4
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.
answered May 23, 2016 at 20:55
0
You can use Symfony Intl Component
Install it via composer using composer require symfony/intl
Then get the HTML symbol with the following
use Symfony\Component\Intl\Currencies;
\Locale::setDefault['en'];
$symbol = Currencies::getSymbol['INR']; // => '₹'
echo $symbol;
answered Apr 18, 2020 at 22:09
aphoeaphoe
2,36625 silver badges29 bronze badges
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.
answered Mar 14, 2014 at 11:02
BenubirdBenubird
17.6k26 gold badges87 silver badges136 bronze badges
Zend_Locale::getTranslationList['CurrencySymbol']
Will give you an associative array of 3 letter currency codes to their symbol.
Can then use like this:
$curArr = Zend_Locale::getTranslationList['CurrencySymbol'];
echo $curArr['GBP'];
answered Mar 16, 2016 at 2:14
Try this variant:
$formatter->getSymbol[NumberFormatter::INTL_CURRENCY_SYMBOL];
answered Feb 7, 2013 at 7:57
0