【发布时间】:2013-10-12 14:07:02
【问题描述】:
我正在尝试解析 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); 将我的文件字符集更改为here。
更多信息
- 网站编码:
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
提前致谢!
【问题讨论】:
-
参考这个链接,因为它会给你更多的选项来处理这些字符php.net/manual/en/function.utf8-encode.php
标签: php html character-encoding