htmlentities 返回空字符串
以下代码输出一个空字符串。原因是 $text 中的“ó”,但为什么呢?那么utf-8编码的是哪些字符呢?
使用iso-8859-1时问题解决了,但我需要使用utf-8,那么我做错了什么?
<!doctype html>
<head>
<meta charset="utf-8">
</head>
<body>
<?
$text = 'Hola ó Hola';
$text = htmlentities($text,ENT_QUOTES,'utf-8');
echo $text;
?>
</body>
</html>
The following code outputs an empty string. The cause is the "ó" in $text, but why? What characters does utf-8 encode then?
The problem is solved when using iso-8859-1, but I need to use utf-8, so what am I doing wrong?
<!doctype html>
<head>
<meta charset="utf-8">
</head>
<body>
<?
$text = 'Hola ó Hola';
$text = htmlentities($text,ENT_QUOTES,'utf-8');
echo $text;
?>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我遇到了类似的问题,并使用标志 ENT_SUBSTITUTE 来防止空字符串。它仍然没有编码,而且我不能依赖 UTF-8 文件,所以我只转换了字符串的编码:
I had a similar issue and used the flag ENT_SUBSTITUTE to prevent the empty string. It still didn't encode, and I couldn't rely on the file being UTF-8, so I converted the encoding on just the string:
如果源文件包含该字符串,请确保将其保存为 UTf-8。否则,请确保提供字符串的任何内容都将其作为 UTF-8 提供。
Make sure you save your source file as UTf-8 if it contains the string. Else make sure that whatever is supplying the string supplies it as UTF-8.
新建>另存为..
输入文件名(暂时空白),然后输入选择PHP Hypertext ...
New > Save As..
put the name of file (blank for a while) and in type selectPHP Hypertext ...
只是为可能与我有类似问题的其他人发表评论。 $var 包含特殊字符。这……
给了我一个空的输出,而……
工作得很好。与 htmlspecialchars 相同。以前从未遇到过这种情况,因为我通常不会那样使用它,但在这里我只是测试一些东西。
Just commenting for others who may have a similar problem as myself. $var containing special characters. This …
… gave me an empty output, while …
… worked fine. Same with htmlspecialchars. Never encountered this before because I normally don't use it that way but here I was just testing something out.