Inside the DOM of PHP is the UTF8 mechanism. When loadhtml, the encoding is set by checking the charset of the character Meta. If there is no charset, it will be treated as iso8859. In this case, when the savexml, the output is UTF8, so see garbled.
Is that not quite understood, for example:
$xml = new DOMDocument ();
@ $xml->loadhtml (' <div> I'm testing to see-http://www.111cn.net</div> ');
$dom = new Domxpath ($xml);
echo $dom->query ('//div ')->item (0)->savexml ();
Open Web page execution, you will find the output garbled. So how do you solve the problem? There are two different ways.
First: Specify the encoding when loadhtml, the following code refers to the reply from the Php.net official document
$doc = new DOMDocument ();
$doc->loadhtml (' <?xml encoding= ' UTF-8 ' > '. $html);
Dirty fix
foreach ($doc->childnodes as $item)
if ($item->nodetype = = Xml_pi_node)
$doc->removechild ($item); Remove hack
$doc->encoding = ' UTF-8 '; Insert Proper
The second method, which converts the character of the output through Iconv, is as follows:
Echo iconv ("UTF-8", "Gb18030//translit", $dom->savexml ($n));