PHP DOMDocument to save XML when Chinese is garbled

Source: Internet
Author: User

Inside the DOM of PHP is the UTF8 mechanism. When loadhtml, the encoding is set by checking the charset of the character Meta. If there is no charset, it will be treated as iso8859. In this case, when the savexml, the output is UTF8, so see garbled.

Is that not quite understood, for example:

$xml = new DOMDocument ();
@ $xml->loadhtml (' <div> I'm testing to see-http://www.111cn.net</div> ');

$dom = new Domxpath ($xml);
echo $dom->query ('//div ')->item (0)->savexml ();

Open Web page execution, you will find the output garbled. So how do you solve the problem? There are two different ways.

First: Specify the encoding when loadhtml, the following code refers to the reply from the Php.net official 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 Proper

The second method, which converts the character of the output through Iconv, is as follows:

Echo iconv ("UTF-8", "Gb18030//translit", $dom->savexml ($n));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.