html_entity_decode(): 不支持字符集“ASCII”

html_entity_decode(): charset `ASCII' not supported

我的 php.log(PHP 错误的日志)每天都用以下行填充:

PHP Warning: html_entity_decode(): charset ASCII not supported, assuming iso-8859-1 in /....php on line 149

第 149 行看起来像:

$html = html_entity_decode($html, ENT_QUOTES, mb_detect_encoding($html));

html_entity_decode()的用法是根据php.net,所以我错过了什么?

您理所当然地认为 mb_detect_encoding() 是一种用于确定文本使用的编码的工具。事实上不是。自动编码检测是不可能完成的,那个功能连尝试都没有。

你得到了ASCII。然后要求 PHP 将 HTML 个实体解码为 US-ASCII。 由于旧的 7 位 US-ASCII 几乎无法存储大约一百个可打印字符,因此这几乎是一项不可能完成的任务。

确定HTML编码的正确方法是:

  1. Web 服务器的 Content-Type header,如果可用:

    Content-Type: text/html; charset=UTF-8
    
  2. HTML 元标记否则:

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta charset="utf-8">