PHP money_format(); £ sign not GBP

痴心易碎 提交于 2019-12-21 07:25:11

问题


I cannot work out how to get the currency symbol?

At the moment I am using

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

Which give me the output

GBP1,000

But I want

£1,000

I have checked out the PHP manual but it isn't that helpful.

Any ideas?


回答1:


Have you tried this?

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



回答2:


This worked for me:

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

It's similar to the selected solution, however it didn't work for me. Why? The reason is that the locale en_GB was not defined in my system, only en_GB.UTF-8:

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

In addition, by using the UTF-8 codeset directly, the extra call to utf8_encode can be saved.




回答3:


Here's a solution for those of you, like me, who think PHP's money_format is horrible.

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



回答4:


None of the soloutions above worked for me. They were printing an 'A' before the £ sign. Instead building on Aidan and Diegos soloutions I have the following:

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



回答5:


An easy solution could be te replace GBP with & pound ; (without the spaces) after the money_format.




回答6:


Use str_replace() function is an option.

£ - British Pound - £ (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);


来源:https://stackoverflow.com/questions/9019337/php-money-format-%c2%a3-sign-not-gbp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!