']; 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.

Chinese characters are garbled when PHP DOMDocument saves xml.

coldplay.xixi
coldplay.xixiOriginal
2020-08-05 15:04:282257browse

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.

Chinese characters are garbled when PHP DOMDocument saves xml.

##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(&#39;<?xml encoding="UTF-8">&#39; . $html);
 
// dirty fix
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 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:

PHP programming from entry to proficiency

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!

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