php htmlentities 和 é (e 急性)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7663738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:07:07  来源:igfitidea点击:

htmlentities and é (e acute)

phpcharacter-encodinghtml-entities

提问by Michael Irigoyen

I'm having a problem with PHP's htmlentitiesand 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.

我遇到了 PHPhtmlentities和 é 字符的问题。我知道这是我忽略的某种编码问题,所以希望有人能看到我做错了什么。

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.

直接运行htmlentities("é")不会按预期返回正确的代码(é或者é。我已经尝试将字符集强制为“UTF-8”(使用 htmlentities 的字符集参数)但同样的事情。

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 ??.

最终目标是将此字符发送到以“ISO-8859-1”编码的 HTML 电子邮件中。当我尝试强制它进入该编码时,同样的问题。在电子邮件的源代码中,您会看到 é,并在 HTML 视图中看到??

Who can shed some light on my mistake?

谁能解释一下我的错误?

回答by

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

回答by Marius Solbakken Mellum

I suggest you take a look at http://php.net/html_entity_decode. You can use this in the following way:

我建议你看看http://php.net/html_entity_decode。您可以通过以下方式使用它:

$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

这样你就不用关心 php 文件的编码了。编辑:错字

回答by genesis

I have added htmlspecialchars for you to see that it is really encoded

我已经为你添加了 htmlspecialchars 以查看它是否真的被编码

http://sandbox.phpcode.eu/g/11ce7/4

http://sandbox.phpcode.eu/g/11ce7/4

<?PHP
echo htmlspecialchars(htmlentities("é", ENT_COMPAT | ENT_HTML401, "UTF-8"));

回答by Ghazanfar Mir

If you have stored the special characters as é, then you could use the following soon after making connection to the database.

如果您已将特殊字符存储为 é,那么您可以在连接到database.

mysql_set_charset('utf8', $dbHandler); 

With this, you now don't need to use htmlentitieswhile displaying data.

有了这个,您现在不需要htmlentities在显示数据时使用。