Monday, October 5, 2009

Pound Sign with Accented A in PHP -- Solved

I had this weird problem in one of the PDFs that was getting generated through ezPDF. The problem was that the price in that page was appearing as a pound character with an accented A before it. My code looked like
$pound_sign = '£';
.....
$pdf->ezText($pound_sign.$somevalue);

On most of the pages, this appeared properly as a pound sign followed by value but in some pages, it wasnt. After lot of debugging and googling around, the problem seems to be one of encoding. If there is a pound sign and its interpreted as a UTF-8 character, it is represented by a 2-byte sequence
   0xC2 0xA3

0xC2 is the accented A character and 0xA3 is the one for the pound sign, but if its interpreted as a ISO-8859-1 (Latin) encoding, it is represented as
   0xA3

which is what we want.

So, to solve the problem, something like

   $pound_sign = html_entity_decode("£", ENT_COMPAT, 'ISO-8859-1');

worked and the accented A disappeared. This is because Im forcing the encoding to be ISO-8859-1 and the character passed to it in 0xA3.

Hope this helps someone

5 comments:

  1. Thanks for this, it's been driving me crazy!

    ReplyDelete
  2. thank you, great job on this problem!

    im having also problem with € (EURO) symbol,
    was trying to solve it like you did:

    html_entity_decode("€", ENT_COMPAT, 'ISO-8859-1');"

    but it just prints it out like "€".

    could only solve it with replacement strategy (provided on ezpdf manual). any thought on that ?

    ReplyDelete
  3. Great, this was driving me mad...thanks!

    ReplyDelete
  4. This did indeed help someone - me... Thank you :)

    ReplyDelete
  5. Thanks very much 2 days I spent trying to fix that!!! I knew it was the UTF-8/ ISO issue but couldn't figure out how to force ISO in php code.

    ReplyDelete