In php, DOMDocument has no problem with xml operations as long as it is in English, but if it is a Chinese font, there will be garbled characters. below we will introduce some solutions to this problem. the php dom is based on the utf8 mechanism... in php, DOMDocument has no problem with xml operations as long as it is in English, but if it is a Chinese font, there will be garbled characters. below we will introduce some solutions to this problem.
PHP's DOM is based on the utf8 mechanism. during loadHTML, the encoding is set by checking the charset of the meta in the character. if there is no charset, it will be processed by iso8859, in this case, utf8 is output when saveXML is executed, so garbled characters are displayed.
This is not quite understandable. for example:
$ Xml = new DOMDocument (); @ $ xml-> loadHTML ('I'm testing. look.-http://www.phprm.com.
'); $ Dom = new DOMXPath ($ xml); echo $ dom-> query (' // P')-> item (0)-> saveXML ();
When you open the web page and execute it, you will find garbled output. how can this problem be solved? There are two methods.
First:Specify the encoding when loadHTML is used. the following code references the response in the official php.net document. the code is as follows:
$doc = new DOMDocument(); $doc->loadHTML('
' . $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
Method 2:Use iconv to re-convert the output characters. the code is as follows:
echo iconv("UTF-8", "GB18030//TRANSLIT", $dom->saveXML($n) );
Permanent address:
Reprint at will ~ Please bring the tutorial URL ^