Fixing garbled htmlentities output by using the proper charset

If you’re running user-provided text through htmlentities and it’s coming out garbled, it’s likely that the page’s charset doesn’t match the one you used to encode the text.

First, we confirm the encoding charset. Starting with PHP 5.4, the default charset is UTF-8, so assuming you’re running a reasonably modern version of PHP and you haven’t set the default_charset in your php.ini (introduced in PHP 5.6) you’re encoding in UTF-8.

Next, we define the charset for the page. Add this to the page HEAD (or change as noted if it’s already there):

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

And voilà — é becomes é again!