【发布时间】:2012-07-03 18:48:17
【问题描述】:
网络服务器正在使用 utf-8 编码提供响应,所有文件都使用 utf-8 编码保存,我所知道的所有设置都已设置为 utf-8 编码。
这是一个快速程序,用于测试输出是否有效:
<?php
$html = <<<HTML
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Test!</title>
</head>
<body>
<h1>☆ Hello ☆ World ☆</h1>
</body>
</html>
HTML;
$dom = new DOMDocument("1.0", "utf-8");
$dom->loadHTML($html);
header("Content-Type: text/html; charset=utf-8");
echo($dom->saveHTML());
程序的输出是:
<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>Test!</title></head><body>
<h1>☆ Hello ☆ World ☆</h1>
</body></html>
呈现为:
~你好~世界~~
我可能做错了什么?要告诉 DOMDocument 正确处理 utf-8,我需要更具体多少?
【问题讨论】:
-
感谢您提出这个问题,类似的问题是:How to keep the Chinese or other foreign language as they are instead of converting them into codes? 但是您可能会认为这是一个 hack。
-
奇怪的是:php-documentation 说:
The DOM extension uses UTF-8 encoding. Use utf8_encode() and utf8_decode() to work with texts in ISO-8859-1 encoding or Iconv for other encodings.见:php.net/manual/en/intro.dom.php -
自 libxml 版本 2.8.0 起支持 HTML5 元字符集编码声明,因此问题中的代码示例现在可以按预期工作。
-
问题是您指定的是 utf8,但
&#152等 不是 用于 utf8,而是 ANSI。例如,“匕首”是hexutf8.com/?q=e280a0
标签: php utf-8 domdocument