Tag Archives: character set

PHP: How to HTML Entity Decode an Euro Symbol CodeUnit 27 JUN 2011

I came across an annoying bug in my application where for some reason my Euro symbol wasn’t being HTML entity decoded for use in a PDF as opposed to the other available symbols which were. At first I assumed that this might be a bug in the PHP html_entity_decode function, but of course, a quick trip to the official PHP documentation proved me to be completely and utterly wrong.

The problem was in the character set all along!

Most of the time we simply run html_entity_decode by passing it the string we want decoded and perhaps the flag which controls whether or not to affect quotes. However, there is a third, often overlooked parameter which controls the character set which PHP needs to use when decoding these HTML entities – and that’s where the trick lies!

By default, if PHP can’t recognise the character set of the string passed to it, it assumes a character set of ISO-8859-1, known as Western European, Latin-1. However, this particular character set omits the Euro sign as well as a few French and Finnish letters, which are all added in ISO-8859-15, or Western European, Latin-9.

So in order to successfully decode our Euro symbol containing string, we simply need to run:

$decoded = html_entity_decode($eurostring,ENT_QUOTES, 'ISO-8859-15');

And now you know.

For a reference, these are the character sets which are supported:

...

MySQL: How to Show the Collation and Character Set of a MySQL Database CodeUnit 25 MAR 2011

To find out what collation and character set your database is using turns out to be pretty trivial thanks to MySQL’s SHOW VARIABLES SQL statement that reveals all the inner workings of your setup.

First, select your database. If you are working from the command line:

USE mydatabase

Then, run the SHOW VARIABLES statement against your database, using a LIKE conditional to narrow down to what we want to see:

SHOW VARIABLES LIKE 'character_set_database'
SHOW VARIABLES LIKE 'collation_database'

These two statements will return the corresponding values that you are interested in.

If you want to see even more of the settings on your database, just run:

SHOW VARIABLES

Nifty.