【问题标题】:While decoding the html entities from string, special characters display incorrectly从字符串解码 html 实体时,特殊字符显示不正确
【发布时间】:2014-02-15 09:21:08
【问题描述】:
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');

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

【问题讨论】:

    标签: php decoding


    【解决方案1】:

    尝试使用 echo 强制显示字符集...

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

    【讨论】:

      【解决方案2】:

      对我来说,用于 html_entity_decode 的 UTF-8 字符集 agrument 工作得很好。在您的 phpfiddle 脚本上进行了测试。 如果不是,请尝试使用 header('Content-Encoding: UTF-8'); 设置内容编码标头

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

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

      【讨论】:

      • 我看到您尝试使用的代码来自 PHP 手册页并且已有 9 年历史。此后 html_entity_decode 的行为得到了极大的改进,因此它可以与 UTF-8 编码一起使用。 2号的问题。在您的示例中调用是您将 UTF-8 参数作为第二个参数而不是第三个参数传递。
      猜你喜欢
      • 1970-01-01
      • 2017-07-05
      • 1970-01-01
      • 1970-01-01
      • 2018-06-02
      • 1970-01-01
      • 2012-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多