【问题标题】:PHP money_format(); £ sign not GBPPHP 金钱格式(); £ 符号不是英镑
【发布时间】:2012-01-26 14:12:03
【问题描述】:

我不知道如何获得货币符号?

目前我正在使用

setlocale(LC_MONETARY, 'en_GB');
money_format('%i', 1000);

这给了我输出

GBP1,000

但我想要

£1,000

我已经查看了 PHP 手册,但它没有那么有用。

有什么想法吗?

【问题讨论】:

  • @Tim 那只是告诉你放 £而不是£,但我实际上可以得到£来显示。它只是在它的位置显示英镑。不过谢谢:)
  • 我的机器甚至不打印GBP。 :o
  • @Shiplu 您的机器可能没有为“en_GB”安装语言环境文件。如果是 Debian 发行版,您可以编辑 /etc/locale.gen
  • @LinusKleen,它的 Ubuntu Lucid。你知道我该怎么做吗?

标签: php money-format


【解决方案1】:

你试过了吗?

setlocale(LC_MONETARY, 'en_GB');
utf8_encode(money_format('%n', 1000));

【讨论】:

  • 我想这很接近了,我现在得到了一颗带有 ?在中间。我的字符集为 utf-8,这很奇怪:S
  • 只需添加 utf8_encode(money_format('%n', 1000));你会看到这个符号... :)
  • 附言。您应该编辑您的答案以包含 utf8_encode 位:)
【解决方案2】:

这对我有用:

setlocale(LC_MONETARY, 'en_GB.UTF-8');
money_format('%n', 1000);

它类似于所选的解决方案,但它对我不起作用。为什么?原因是我的系统中没有定义语言环境en_GB,只有en_GB.UTF-8

$ locale -a | grep "en_GB"
en_GB.utf8

另外,通过直接使用 UTF-8 代码集,可以节省对utf8_encode 的额外调用。

【讨论】:

  • 比选择的答案灵活得多,因为如果使用不同的位置,这将不再需要重写函数。
【解决方案3】:

对于像我这样认为 PHP 的 money_format 很糟糕的人来说,这里有一个解决方案。

$formatted = '£' . number_format( (float) $amount, 2, '.', ',' );

【讨论】:

    【解决方案4】:

    我知道这是一个旧线程,但我发现的方式不使用现在已弃用的 money_format

    $fmt = numfmt_create( 'en_GB', NumberFormatter::CURRENCY );
    $formattedAmount = numfmt_format_currency($fmt, 1000, "GBP");
    echo $formattedAmount;
    

    将显示 £1,000.00

    如果您需要从末尾删除 .00,那么您可以使用

    substr(formattedAmount, 0, -3);
    

    【讨论】:

      【解决方案5】:

      以上解决方案都不适合我。他们在£ 符号之前打印了一个“A”。相反,建立在 Aidan 和 Diegos 的解决方案上,我有以下几点:

      setlocale(LC_MONETARY, 'en_GB.UTF-8');
      echo (money_format('%n', $bagel->Price)); 
      

      【讨论】:

        【解决方案6】:

        一个简单的解决方案是将 GBP 替换为 & pound ; (不带空格)在 money_format 之后。

        【讨论】:

        • 在他们显示的 php 手册上,您可以使用 $ 符号,因此您必须能够获得 £,因为您可以将您的位置设置为 GB
        • 您是否尝试过使用setlocale(LC_MONETARY, 'en_GB.UTF-8');。建议在@tim 网站上作为评论放置。
        【解决方案7】:

        使用 str_replace() 函数是一个选项。

        £ - 英镑 - £ (163)

        // Search for the GBP in your string (subject) then replace for the symbol code
        $search = "GBP";
        $replace = "£";
        $subject = "GBP";
        echo str_replace($search, $replace, $subject);
        

        【讨论】:

        • 这只对在 HTML 页面上显示才有意义
        猜你喜欢
        • 2016-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多