']; 2. Use iconv to Output characters are reconverted."/> ']; 2. Use iconv to Output characters are reconverted.">
Home >Backend Development >PHP Problem >Chinese characters are garbled when PHP DOMDocument saves xml.
Solution to the problem that Chinese characters appear garbled when PHP DOMDocument saves xml: 1. Specify the encoding when loading HTML, the code is [$doc->loadHTML('']; 2. Use iconv to output The characters are re-converted.
##Solution to the problem that Chinese characters appear garbled when PHP DOMDocument saves xml:
The first method: Specify the encoding when loading HTML. The following code is quoted from the reply in the official php.net 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 properThe second method is to re-convert the output characters through iconv. The code is as follows:
echo iconv("UTF-8", "GB18030//TRANSLIT", $dom->saveXML($n) );
Related video recommendations:
The above is the detailed content of Chinese characters are garbled when PHP DOMDocument saves xml.. For more information, please follow other related articles on the PHP Chinese website!