UTF-8 not working? Look for htmlentities.

I converted all my sites to UTF-8 and it was relatively painless. However, on one of my sites, I kept seeing legal UTF-8 characters turning into gibberish. It happened after entering UTF-8, submitting a form and then redisplaying the values in an edit box.

I know that all my pages are using UTF-8, and also that my database stores the text as UTF-8.

The thing that I overlooked is that, because I’m writing text to an input box, I’m using PHP’s htmlentities function to handle special characters, and the default character set used by that function is ISO-8859-1. I had to change my PHP command from

echo htmlentities($field);


echo htmlentities($field, ENT_COMPAT, 'UTF-8');

Once I verified that worked, I searched my whole codebase for htmlentities and made sure they were all using ‘UTF-8’

This entry was posted in Character encoding, Debugging, Web programming. Bookmark the permalink.

2 Responses to UTF-8 not working? Look for htmlentities.

  1. John says:

    You can also just change you php.ini file. After you make the change, be sure to restart your web browser.
    ;default_charset = “iso-8859-1”

    default_charset = “utf-8”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s