htmlentities 和 é (e 急性)

发布于 2024-12-08 11:30:38 字数 357 浏览 0 评论 0原文

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

直接运行 htmlentities("é") 不会按预期返回正确的代码(éé我尝试强制字符集为“UTF-8”(使用 htmlentities 的字符集参数),但

最终目标是在 HTML 电子邮件中编码发送该字符 。 “ISO-8859-1”。当我尝试将其强制转换为该编码时,您会在电子邮件的源代码中看到 é,并在 HTML 视图中看到 é

。对我的错误有一些了解吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

回眸一笑 2024-12-15 11:30:38
// 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 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);
与之呼应 2024-12-15 11:30:38

我添加了 htmlspecialchars 供您查看它是否真正编码

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

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

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"));
乙白 2024-12-15 11:30:38

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

$eacute = html_entity_decode('é',ENT_COMPAT,'iso-8859-1');

这样您就不必关心 php 文件的编码。
编辑:错别字

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

岁月静好 2024-12-15 11:30:38

我修复了

$string = htmlentities($string,ENT_QUOTES | ENT_SUBSTITUTE,"ISO-8859-1");

I fixed with

$string = htmlentities($string,ENT_QUOTES | ENT_SUBSTITUTE,"ISO-8859-1");
此岸叶落 2024-12-15 11:30:38

如果您将特殊字符存储为 é,那么您可以在连接到数据库后立即使用以下内容。

mysql_set_charset('utf8', $dbHandler); 

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文