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
Release: 2023-03-03 20:40:01
Original
2245 people have browsed it

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
Copy after login

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) );
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template