']; 2. Use iconv to Output characters are reconverted."/> ']; 2. Use iconv to Output characters are reconverted.">
search
HomeBackend DevelopmentPHP ProblemChinese 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.

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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Hot Topics