htmlentities and é (e acute)

人走茶凉 提交于 2019-12-09 11:37:20

问题


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?


回答1:


// 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);



回答2:


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




回答3:


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"));



回答4:


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.



来源:https://stackoverflow.com/questions/7663738/htmlentities-and-%c3%a9-e-acute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!