What to do if php domdocument has Chinese garbled characters

藏色散人
Release: 2023-03-04 16:08:01
Original
3387 people have browsed it

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

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

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

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

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!

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