17

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?

Max Rose-Collins
  • 1,904
  • 5
  • 26
  • 45

7 Answers7

23

Have you tried this?

setlocale(LC_MONETARY, 'en_GB');
utf8_encode(money_format('%n', 1000));
SERPRO
  • 10,015
  • 8
  • 46
  • 63
22

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.

Diego Pino
  • 11,278
  • 1
  • 55
  • 57
  • Much more flexible than the selected answer, as this stops the need to rewrite functions if using different localitys. – rickyduck Mar 21 '13 at 13:34
4

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

$formatted = '£' . number_format( (float) $amount, 2, '.', ',' );
Daniel Howard
  • 4,330
  • 3
  • 30
  • 23
1

I know this is an old thread but the way I found without using money_format which is now deprecated

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

will show £1,000.00

If you need to remove the .00 from the end then you could use

substr(formattedAmount, 0, -3);
a.cayzer
  • 289
  • 4
  • 22
0

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)); 
atoms
  • 2,993
  • 2
  • 22
  • 43
-1

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

Aidan
  • 118
  • 1
  • 11
  • On the php manual they show you can use the $ sign so you must be able to get the £ as you can set your location to GB – Max Rose-Collins Jan 26 '12 at 14:22
  • 1
    Have you tried using `setlocale(LC_MONETARY, 'en_GB.UTF-8');`. It was sugested on the website @tim placed as a comment. – Aidan Jan 26 '12 at 14:26
-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);
devasia2112
  • 5,844
  • 6
  • 36
  • 56