Php 从字符串解码html实体时,特殊字符显示不正确

Php 从字符串解码html实体时,特殊字符显示不正确,php,decoding,Php,Decoding,我正在使用上面的函数从字符串中解码HTML实体,但在解码特殊字符时显示不正确,如�。 对我来说,用于html实体解码的UTF-8字符集格式工作得很好。在phpfiddle脚本上测试。 如果不是,请尝试使用header('content-encoding:UTF-8')设置内容编码头 考虑到示例中错误的参数位置,适用于我的代码如下所示: function decode_entities($text) { $text= html_entity_decode($text,ENT_QUOTES,

我正在使用上面的函数从字符串中解码HTML实体,但在解码特殊字符时显示不正确,如

对我来说,用于html实体解码的UTF-8字符集格式工作得很好。在phpfiddle脚本上测试。 如果不是,请尝试使用
header('content-encoding:UTF-8')设置内容编码头

考虑到示例中错误的参数位置,适用于我的代码如下所示:

function decode_entities($text) {
    $text= html_entity_decode($text,ENT_QUOTES,"ISO-8859-1"); #NOTE: UTF-8 does not work!
    $text= preg_replace('/&#(\d+);/me',"chr(\\1)",$text); #decimal notation
    $text= preg_replace('/&#x([a-f0-9]+);/mei',"chr(0x\\1)",$text);  #hex notation
    return $text;
}

echo decode_entities("For tiden er neste president i det afrikanske landet Burkina Faso 11 år
");

echo html_entity_decode("For tiden er neste president i det afrikanske landet Burkina Faso 11 år
",'UTF-8');

尝试使用回音强制显示字符集

header('Content-Encoding: UTF-8');
echo html_entity_decode("For tiden er neste president i det afrikanske landet Burkina Faso 11 år", ENT_QUOTES, 'UTF-8');
echo”“;
echo html_entity_decode(“适用于南非共和国驻布基纳法索共和国总统蒂登·内斯特11号,'UTF-8');

我看到您尝试使用的代码来自PHP手册页面,已经有9年历史了。此后,html_实体_解码的行为得到了极大的改进,因此它可以与UTF-8编码一起工作。第二个问题。示例中的调用是将UTF-8参数作为第二个参数传递,而不是第三个参数。
echo "<meta charset='UTF-8'>";
echo html_entity_decode("For tiden er neste president i det afrikanske landet Burkina Faso 11 &aring;r",'UTF-8');