Home >Backend Development >PHP Tutorial >Solution to the problem of garbled Chinese characters when DOMDocument saves xml in PHP

Solution to the problem of garbled Chinese characters when DOMDocument saves xml in PHP

高洛峰
高洛峰Original
2016-12-22 14:35:131850browse

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(&#39;<div>我就是测试看看</div>&#39;);
$dom = new DOMXPath($xml);
echo $dom->query(&#39;//div&#39;)->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(&#39;<?xml encoding="UTF-8">&#39; . $html);
foreach ($doc->childNodes as $item)
{
if ($item->nodeType == XML_PI_NODE)
{
$doc->removeChild($item); // remove hack
}
}
$doc->encoding = &#39;UTF-8&#39;; // 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!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn