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 encod开发者_JAVA百科ing, same issue. In the source of the email, you see é, and in the HTML view é
.
Who can shed some light on my mistake?
// 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);
I have added htmlspecialchars for you to see that it is really encoded
http://sandbox.phpcode.eu/g/11ce7/4
<?PHP
echo htmlspecialchars(htmlentities("é", ENT_COMPAT | ENT_HTML401, "UTF-8"));
I suggest you take a look at http://php.net/html_entity_decode . You can use this in the following way:
$eacute = html_entity_decode('é',ENT_COMPAT,'iso-8859-1');
This way you don't have to care about the encoding of the php file. edit: typo
I fixed with
$string = htmlentities($string,ENT_QUOTES | ENT_SUBSTITUTE,"ISO-8859-1");
If you have stored the special characters as é, then you could use the following soon after making connection to the database
.
mysql_set_charset('utf8', $dbHandler);
With this, you now don't need to use htmlentities
while displaying data.
精彩评论