Search code examples
phpcharacter-encodinghtml-entities

htmlentities and é (e acute)


I'm having a problem with PHP's htmlentities and the é character. I know it's some sort of encoding issue I'm just overlooking, so hopefully someone can see what I'm doing wrong.

Running a straight htmlentities("é") does not return the correct code as expected (either é or é. I've tried forced the charset to be 'UTF-8' (using the charset parameter of htmlentities) but the same thing.

The ultimate goal is to have this character sent in an HTML email encoded in 'ISO-8859-1'. When I try to force it into that encoding, same issue. In the source of the email, you see é, and in the HTML view é.

Who can shed some light on my mistake?


Solution

  • // I assume that your page is utf-8 encoded
    header("Content-type: text/html;charset=UTF-8");
    
    $in_utf8encoded = "é à ù è ò";
    
    // first you need the convert the string to the charset you want...
    $in_iso8859encoded = iconv("UTF-8", "ISO-8859-1", $in_utf8encoded);
    
    // ...in order to make htmlentities work with the same charset
    $out_iso8859= htmlentities($in_iso8859encoded, ENT_COMPAT, "ISO-8859-1");
    
    // then only to display in your page, revert it back to utf-8
    echo iconv("ISO-8859-1", "UTF-8", $out_iso8859);