PHP DOMDocument未正确呈现Unicode字符

Mun*_*nib 6 php unicode domdocument

我使用CKEditor让用户发表评论,用户也可以将unicode字符放在评论框中.

当我提交表格并检查$ _POST ["回复"]时,unicode字符显示得非常好.我也在header('Content-type:text/html; charset=utf-8');页面顶部使用但是当我使用PHP DOMDocument处理它时,所有字符都变得不可读.

$html_unicode = "xyz unicode data";
$html_data = '<body>'.$html_unicode . '</body>';
$dom = new DOMDocument();
$dom->loadHTML($html_data );

$elements = $dom->getElementsByTagName('body');
Run Code Online (Sandbox Code Playgroud)

当我回声

echo $dom->textContent;
Run Code Online (Sandbox Code Playgroud)

输出变为

§Ø³ÙبÙÙ ÙÙÚº ØºØ±ÙØ¨ ک٠آÙÛ ÙÛÙ
Run Code Online (Sandbox Code Playgroud)

如何使用PHP DOMDocument获取正确的unicode字符.

And*_*dre 15

这对我有用:

$html_unicode = "xyz unicode data";
$html_data = '<body>'.$html_unicode . '</body>';

$dom = new DOMDocument();
$html_data  = mb_convert_encoding($html_data , 'HTML-ENTITIES', 'UTF-8'); // require mb_string
$dom->loadHTML($html_data);

$elements = $dom->getElementsByTagName('body');
Run Code Online (Sandbox Code Playgroud)


Roh*_*edi 5

尝试这个 :)

<?php
    $html_unicode = "xyz unicode data";
    $html_data = '<body>'.$html_unicode . '</body>';
    $dom = new DOMDocument();
    $dom->loadHTML($html_data );

    $elements = $dom->getElementsByTagName('body');
    echo utf8_decode($dom->textContent);
?>
Run Code Online (Sandbox Code Playgroud)


Mun*_*nib 1

感谢上帝,我只需更换就得到了解决方案

$html_data = '<body>'.$html_unicode . '</body>';
Run Code Online (Sandbox Code Playgroud)

$html_data = '<head><meta http-equiv="Content-Type" 
content="text/html; charset=utf-8">
</head><body>' . $html_unicode . '</body>';
Run Code Online (Sandbox Code Playgroud)