简体   繁体   English

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

[英]While decoding the html entities from string, special characters display incorrectly

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

I am using the above function to decode HTML entities from the string but while decoding special characters are displaying incorrectly like . 我正在使用上面的函数从字符串中解码HTML实体,但是在解码特殊字符 显示不正确。 Demo 演示

Try use a echo to force the displayed charset... 尝试使用回声强制显示的字符集...

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

For me, the UTF-8 charset agrument for html_entity_decode works just fine. 对我来说,html_entity_decode的UTF-8字符集很合适。 Tested on your phpfiddle script. 在您的phpfiddle脚本上进行了测试。 If it's not, try setting a content-encoding header using header('Content-Encoding: UTF-8'); 如果不是,请尝试使用header('Content-Encoding: UTF-8');设置内容编码标header('Content-Encoding: UTF-8');

Considering the wrong parameter place in the example, the code that works for me looks like this: 考虑到示例中错误的参数位置,适合我的代码如下所示:

header('Content-Encoding: UTF-8');
echo html_entity_decode("For tiden er neste president i det afrikanske landet Burkina Faso 11 &aring;r", ENT_QUOTES, 'UTF-8');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM