Skip to content
Advertisement

How to convert accented chars to html in php?

I have foreign language strings stored in utf8. When displaying them, I want them to be transform in such a way that accented chars become their html counterparts, e.g. é becomes é

However I phrase my search, all I can find is htmlentities() which does not do it.

Advertisement

Answer

Make sure you’re properly specifying the encoding when you call htmlentities().

The documentation shows that it defaults to 'UTF-8', but if you read down a bit further, you’ll see that that’s new as of PHP 5.4. If you’re using an older version, the default is actually 'ISO-8859-1', and you’ll need to make sure you explicitly specify it as 'UTF-8' instead of relying on the default behaviour.

htmlentities($string, 0, 'UTF-8');
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement