PHP HTML encoding


PHP HTML encoding

我正在尝试解析HTML页面,但编码弄乱了我的结果。经过一些研究,我发现了一个非常流行的解决方案,使用 utf8_encode()utf8_decode() ,但它并没有改变任何东西。在以下行中,您可以检查我的代码和输出。

法典

$str_html = $this->curlHelper->file_get_contents_curl($page);
$str_html = utf8_encode($str_html);
$dom = new DOMDocument();
$dom->resolveExternals = true;
$dom->substituteEntities = false;
@$dom->loadHTML($str_html);
$xpath = new DomXpath($dom);
(...)
$profile = array();
for ($index = 0; $index < $table_lines->length; $index++) {
    $desc = utf8_decode($table_lines->item($index)->firstChild->nodeValue);
}

输出

Testar é bom

应该是

Testar é bom

我尝试过什么

  • htmlentities((:

    htmlentities($table_lines->item($index)->lastChild->nodeValue, ENT_NOQUOTES, ini_get('ISO-8859-1'), false);

  • htmlspecialchars((:

    htmlspecialchars($table_lines->item($index)->lastChild->nodeValue, ENT_NOQUOTES, 'ISO- 8859-1', false);

  • 更改此处描述的文件字符集。

更多信息

  • 网站编码:<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />

提前感谢!

尝试在没有事先utf8_decode()的情况下使用以下方法:

mb_convert_encoding($str, 'ISO-8859-1', 'UTF-8');

或者,不要使用utf8_decode()并尝试将您的网站元更改为:

<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

  • mb_convert_encoding()