4

Possible Duplicate:
Encoding issue: £ pound symbol appearing as <?> symbol

I am doing some basic echo statements and when i use echo "£" it puts a capital A before the pound sign any ideas why?

 echo ("£"):

 output = A£

the A has a symbol above.

Community
  • 1
  • 1
mitchnufc
  • 309
  • 2
  • 3
  • 13
  • 1
    You have an encoding issue. It's probably not actually `A`, but `Ã` (IIRC), which smells of UTF-16 being interpreted as ASCII. – Lightness Races in Orbit Mar 20 '12 at 23:34
  • @CarrieKendall, that looks like a MySQL-specific encoding issue. I'm sure there's a duplicate somewhere, but it's awfully difficult for the OP to know what to look for if he doesn't know about character encodings. – benesch Mar 20 '12 at 23:40
  • 2
    @LightnessRacesinOrbit I see it as `£`. – Neil Mar 20 '12 at 23:44
  • [What is a character encoding, and why should I care?](http://www.w3.org/International/questions/qa-what-is-encoding) – hakre Mar 23 '12 at 15:39

3 Answers3

4

There are several ways to do this pragmatically:

<?php
echo chr(163);
printf("%c", 163);
echo "&pound;"; //preferred for HTML
?>

Another way to do this more "manually" would be to save your files in UTF-8 encoding.

See here for more information.

Here's a screenshot of the results:

results

As stated by primatology, if you're going to be ouputting to HTML, make sure to include the proper encoding header for HTML. This should be inserted between your <head> tags.

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Shea
  • 1,965
  • 2
  • 18
  • 42
  • Your first two suggestions are broken, as they assume a certain encoding on the client-side. And "more prefered" is not valid English. – Lightness Races in Orbit Mar 20 '12 at 23:51
  • 1
    if i use echo "&pound"; it should output a £? – mitchnufc Mar 20 '12 at 23:55
  • @andrewjackson: You're watching it "work" in one very specific scenario, with no regard for the many, many other potential cases. As I said, you've assumed a certain encoding, and then gone on to only test with that encoding. It's not an encoding-agnostic solution. It's not a robust solution. `£` is your best bet. – Lightness Races in Orbit Mar 20 '12 at 23:57
  • well, yes... choose just one method – Shea Mar 20 '12 at 23:57
  • @andrewjackson: Thank you for your well-formed question. Unfortunately, it gives me no idea what part of my comments you have failed to understand. Perhaps you do not realise that a mapping of `163` to the UK Pound Symbol is encoding-defined? Best of luck in the future. – Lightness Races in Orbit Mar 20 '12 at 23:58
  • @mitchnufc yes, that is the preferred method for outputting to HTML. – Shea Mar 21 '12 at 00:03
  • Despite the ease of this solution, I wouldn't recommend it. [You should avoid entities](http://stackoverflow.com/a/436637/1122351) unless you are escaping "<", ">", or "&". (Using the actual symbol is much more user-friendly.) You have an encoding issue *somewhere*; best to solve the problem now. – benesch Mar 21 '12 at 00:28
  • @primatology: Your hint is misleading, because if you use HTML, you just want to use the default encoding which *is not UTF-8*. Also please don't stress users into UTF-8 if you can't give a guidline of pros and cons. I better suggest the W3C resources in that case, e.g. [What is a character encoding, and why should I care?](http://www.w3.org/International/questions/qa-what-is-encoding) – hakre Mar 23 '12 at 15:34
  • @hakre, then let me point you to a W3C resource: [Which character encoding should I use for my content, and how do I apply it to my content?](http://www.w3.org/International/questions/qa-choosing-encodings#answer). The HTML5 spec recommends UTF-8 and discourages users from any other character encoding. Thus I maintain my recommendation. Granted, perhaps a little more explanation is in order. But for a beginner, "use UTF-8 and use symbols, not named entities" is usually much less confusing than trying to grok character encodings. – benesch Mar 23 '12 at 17:42
  • especially helpful. – Sinister Beard Aug 14 '15 at 13:40
1

You have to encode the php file as UTF-8 or use

&pound;
Julian Hinderer
  • 169
  • 3
  • 10
0

Make sure your HTML page is marked as UTF-8 (unicode charset). For bulletproof support, place this in your head:

<meta charset="utf-8"> 

You should also save the file with a UTF-8 encoding—most text editors have a built-in capability for this.


If on Apache, you can also send the UTF-8 encoding in the Content-Type header by adding this to a .htaccess:

AddDefaultCharset utf-8
benesch
  • 5,239
  • 1
  • 22
  • 36
  • I have that in the head of my html files as standard – mitchnufc Mar 20 '12 at 23:42
  • Note that `` is an HTML5 meta tag, some older browsers may only recognise the old `` tag. – Neil Mar 20 '12 at 23:42
  • @mitcnufc, then you've got an encoding issue somewhere along the pipeline. Check to make sure the file is actually *saved* as UTF-8, and inspect the headers (using Firebug or Safari's Dev Tools) to see if your server's overriding the meta tag. – benesch Mar 20 '12 at 23:45
  • Actually, @Neil, I think support is [universal](http://web.archive.org/web/20090214110633/http://code.google.com/p/doctype/wiki/MetaCharsetAttribute), and that's a few years out of date now. But the old tag is certainly worth a shot. – benesch Mar 20 '12 at 23:49