Get currency symbol in PHP
Asked Answered
F

9

54

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?

Faeroese answered 16/12, 2012 at 0:35 Comment(7)
The problem is that the currency symbol can appear either before or after the number. What are you using the symbol for?Chihli
Just do a regex for anything thats not a digit or a comma. Something like /[^0-9,]*/.Antifederalist
Some of the symbols will be multi-byte so a simple string search won't work very well.Nothing
I want to avoid regex. Actually, I wanted to fix github.com/symfony/symfony/blob/master/src/Symfony/Component/… and get rid of this regex, I thought PHP supports this. Thanks anyway.Faeroese
The most useful related resource I have found is this (yes, it's python, but all those functions are available in PHP), which is a bit of a nasty "solution". The root of the problem is that this functionality is dependent on the host OS having the appropriate locale information available - but at the same time I don't see a reason for this not to be possible within those restrictions.Bet
@Bet I tried and currency_symbol and most of other data is empty for some reason. Thanks.Faeroese
Warning: NumberFormatter::CURRENCY has been deprecated since PHP 8.3Desiderata
F
27

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'); 
Faeroese answered 5/6, 2013 at 14:10 Comment(4)
Technically (IMHO) this doesn't actually answer the question, which was about PHP, not Symfony.Januarius
@Januarius I asked this question, and the goal is to get currency symbol in PHP. Using third party libraries is allowed and encouraged.Faeroese
Also, what a beautiful syntax.Housebreak
See Federico Nicolas Motta's answer.Redhanded
W
61

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
Wallis answered 4/5, 2015 at 9:30 Comment(1)
Excellent!! +1. I just wish there was a way to retrieve locale based on currency iso. Something like rlocaleconv(int_curr_symbol, 'JPY') or so!Laryssa
F
27

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'); 
Faeroese answered 5/6, 2013 at 14:10 Comment(4)
Technically (IMHO) this doesn't actually answer the question, which was about PHP, not Symfony.Januarius
@Januarius I asked this question, and the goal is to get currency symbol in PHP. Using third party libraries is allowed and encouraged.Faeroese
Also, what a beautiful syntax.Housebreak
See Federico Nicolas Motta's answer.Redhanded
N
5

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);
Nothing answered 16/12, 2012 at 1:16 Comment(4)
Currency symbols may be more than one character, e.g., Estonia Kroon = "kr".Civvies
Currency symbol is not always one char en.wikipedia.org/wiki/Currency_sign. Thanks.Faeroese
@Faeroese please see updated code which supports multi-char symbols.Nothing
Yes, but it's hacky, similar to github.com/symfony/symfony/blob/master/src/Symfony/Component/… I will accept it if I find no other solution. Thanks.Faeroese
T
5

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.

Tankoos answered 23/5, 2016 at 20:55 Comment(0)
H
2

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;
Heterography answered 18/4, 2020 at 22:9 Comment(0)
I
1

I used this code and this code in php

$CS = new NumberFormatter('en_PH', NumberFormatter::CURRENCY);
$CS = $CS->getSymbol(NumberFormatter::CURRENCY_SYMBOL);

I got a currency symbol of Philippine Peso ₱₱₱₱

I also tried HTML code '&#8369' for Philippine Peso ₱. But it does not working when using in php server php mail specially if you are using an email server.

Try this code.

Interbreed answered 24/12, 2022 at 10:28 Comment(0)
B
0

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.

Blithe answered 14/3, 2014 at 11:2 Comment(0)
L
0
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'];
Lenoralenore answered 16/3, 2016 at 2:14 Comment(0)
B
-2

Try this variant:

$formatter->getSymbol(NumberFormatter::INTL_CURRENCY_SYMBOL);
Bonds answered 7/2, 2013 at 7:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.