php - 为什么 PHP 函数 htmlentities(...) 返回错误结果?

标签 php html-entities

我有以下代码:

function testAccents() {
    $str = "àéè";
    $html = htmlentities($str);
    echo $html;
}

当我运行它时,我得到的不是àéè,而是àéè

我认为这可能是编码问题,但文件是 utf-8 :

> file -bi PublicationTest.php 
  text/x-c++; charset=utf-8

为什么我会得到这个奇怪的结果?

编辑:我使用 PHP 5.3。

最佳答案

PHP 5.4.0 之前,htmlentities()默认情况下需要 ISO-8859-1 数据。它将您的 UTF-8 输入解释为单字节字符,这会导致您得到有趣的结果。

具体指定编码。

$html = htmlentities($str, ENT_COMPAT, "UTF-8");

关于php - 为什么 PHP 函数 htmlentities(...) 返回错误结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13246903/

相关文章:

php - 没有 php.ini 与 brew

php - 将值列出到文本字段中

php - 显示 & 而不是 &

javascript - 编码 HTML 实体

SWIFT:+[CATransaction synchronize] 在解码 HTML 实体时在交易中调用

html - unicode 或 HTML 是否有垂直双 guillemet(人字形)?

php - 复杂的 MySQL ORDERing : 'the' and punctuation ignored, 加数字最后

php - PHP 中的多个动态 SEO/缩短的 URL

javascript - 如何让用户返回到使用当前代码的特定 div

php - 在 mb_encode_numericentity() 中更好地解释 $convmap