Get currency symbol in PHP(在 PHP 中获取货币符号)
问题描述
让我们从一段简单的代码开始,用 NumberFormatter
格式化货币:
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');
打印:¥123,456,789
.
如果你想格式化钱,这没关系.
This is ok if you want to format money.
但我想做的是获取给定货币 ISO 4217 代码(例如 JPY)的货币符号(例如 ¥).
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);
但这给出了构造函数 (en_US) 中给出的语言环境的货币符号,在我的例子中是 $.
But that gives currency symbol for locale given in constructor (en_US), $ in my case.
有没有办法在 PHP 中通过货币 ISO 4217 代码获取货币符号?
Is there a way to get currency symbol by currency ISO 4217 code in PHP?
推荐答案
我使用 https://github 实现了这个.com/symfony/Intl:
SymfonyComponentIntlIntl::getCurrencyBundle()->getCurrencySymbol('EUR')
返回
'€'.
Symfony 4.3 >
值得指出的是 SF4.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
{
所以,你可以这样做:
use SymfonyComponentIntlCurrencies;
echo Currencies::getSymbol('AUD');
这篇关于在 PHP 中获取货币符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 PHP 中获取货币符号


基础教程推荐
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01