DOMDocument in PHP has no problem with xml operations as long as it is in English, but if it is Chinese fonts, there will be garbled problems. Let's introduce some solutions to this problem.
PHP's DOM is internally utf8 mechanism , when loading HTML, the encoding is set by checking the charset of the meta in the character. If there is no charset, it will be processed as iso8859. In this case, when savingXML is performed, the output is utf8, so you will see garbled characters.
You don’t quite understand if you say this, for example:
$xml = new DOMDocument(); @$xml->loadHTML('<div>我就是测试看看</div>'); $dom = new DOMXPath($xml); echo $dom->query('//div')->item(0)->saveXML();
Open the web page and execute it, you will find that the output is garbled, so how to solve this problem? There are two ways.
The first method: specify the encoding when loading HTML. The following code is quoted from the reply in the php.net official document. The code is as follows:
$doc = new DOMDocument(); $doc->loadHTML('<?xml encoding="UTF-8">' . $html); 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: through iconv To re-convert the output characters, the code is as follows:
echo iconv("UTF-8", "GB18030//TRANSLIT", $dom->saveXML($n) );
The above is the solution introduced by the editor to you to solve the problem of garbled Chinese characters when DOMDocument saves xml in PHP. I hope it will be helpful to everyone. If you If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank you all for your support of the PHP Chinese website!
For more PHP solutions to the problem of garbled Chinese characters when DOMDocument saves xml, please pay attention to the PHP Chinese website for related articles!