在 PHP 中获取货币符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13897516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:13:33  来源:igfitidea点击:

Get currency symbol in PHP

phpformattingcurrency

提问by umpirsky

Let's start with simple piece of code to format money with NumberFormatter:

让我们从一段简单的代码开始,用以下代码格式化钱NumberFormatter

$formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
echo $formatter->formatCurrency(123456789, 'JPY');

This prints: ¥123,456,789.

这打印:¥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).

但我想要做的是获取给定货币 ISO 4217 代码(例如 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.

但这给出了构造函数(en_US)中给出的语言环境的货币符号,在我的情况下为 $ 。

Is there a way to get currency symbol by currency ISO 4217 code in PHP?

有没有办法在 PHP 中通过货币 ISO 4217 代码获取货币符号?

采纳答案by umpirsky

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

我使用https://github.com/symfony/Intl实现了这一点:

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

returns

返回

''.

回答by idragosalex

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:

正确的方法如您所料,使用 NumberFormatter::CURRENCY_SYMBOL,但您首先必须设置适当的语言环境,如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:

例如,$symbol 将是:

  • Canadian dollar(CAD) : CA$in USA, CADin Romania , $CAin Iran
  • Iran Rial(IRR): IRRin USA, while in Iran will be ?
  • 加元(CAD):CA $在美国,CAD在罗马尼亚,$ CA在伊朗
  • 伊朗里亚尔(IRR):美国的IRR,而伊朗将是?

回答by Federico Nicolas Motta

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.

如果您使用此函数设置区域设置setlocale("LC_ALL", "es_AR");您可以使用localeconv()['currency_symbol']localeconv()['int_curr_symbol']来获取区域设置货币符号和货币符号的国际变体。

回答by cryptic ツ

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.

由于符号可以是多字节的,我使用 mb_*() 函数来正确抓取所有只会留下符号的非标点符号和非数字字符。

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

回答by Benubird

Cryptic has a good answer, but there is a simpler way to do it:

Cryptic 有一个很好的答案,但有一个更简单的方法:

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.

这是一个很好的简单内联解决方案,不需要声明另一个函数,但是它也不能正确处理所有情况 - 即字母是输出一部分的货币。但是为了区分例如 $ 和 £,它工作正常。

回答by aphoe

You can use Symfony IntlComponent

您可以使用Symfony 国际组件

Install it via composer using composer require symfony/intl

使用 Composer 安装它 composer require symfony/intl

Then get the HTML symbol with the following

然后使用以下内容获取 HTML 符号

use Symfony\Component\Intl\Currencies;
\Locale::setDefault('en');
$symbol = Currencies::getSymbol('INR'); // => '?'
echo $symbol;

回答by Rob Clarkson

Zend_Locale::getTranslationList('CurrencySymbol')

Will give you an associative array of 3 letter currency codes to their symbol.

将为您提供一个由 3 个字母货币代码组成的关联数组与其符号。

Can then use like this:

然后可以这样使用:

$curArr = Zend_Locale::getTranslationList('CurrencySymbol');
echo $curArr['GBP'];

回答by zloyrusskiy

Try this variant:

试试这个变体:

$formatter->getSymbol(NumberFormatter::INTL_CURRENCY_SYMBOL);