loadHTML(mb_convert_encoding($profile, 'HTML-ENTITIES','UTF-8'))"."/> loadHTML(mb_convert_encoding($profile, 'HTML-ENTITIES','UTF-8'))".">

Home >Backend Development >PHP Problem >What to do if php domdocument has Chinese garbled characters

What to do if php domdocument has Chinese garbled characters

藏色散人
藏色散人Original
2020-08-17 10:52:533416browse

php The solution to Chinese garbled domdocument: first open the corresponding file; then make an encoding statement, the statement is "$dom->loadHTML(mb_convert_encoding($profile, 'HTML-ENTITIES','UTF-8 '))".

What to do if php domdocument has Chinese garbled characters

Recommended: "PHP Video Tutorial"

Solution to garbled characters in PHP DOMDocument loadHTML

DOMDocument::loadHTML default encoding is ISO-8859-1, so we need to make an encoding statement:

$dom->loadHTML(mb_convert_encoding($profile, 'HTML-ENTITIES', 'UTF-8'));

Complete test code:

$profile = '

イリノイ州シカゴにて、アイルランド系の家庭に、9

'; $dom = new DOMDocument(); $dom->loadHTML(mb_convert_encoding($profile, 'HTML-ENTITIES', 'UTF-8')); echo $dom->saveHTML($dom->getElementsByTagName('div')->item(0));

Another method is to add before the characters of loadHTML Upper encoding statement:

$dom->loadHTML(&#39;<?xml encoding="utf-8" ?>&#39; . $profile);

The above is the detailed content of What to do if php domdocument has Chinese garbled characters. 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