使用 DOMDocument 进行 PHP 编码

2022-01-07 00:00:00 character-encoding dom php
<tag>
Алекс М
</tag>

当我尝试使用 DOMDocument 函数获取以下代码的内容时,它返回如下内容:

When I try to get the content of the following code using DOMDocument functions, it returns something like:

ÐÐ»ÐµÐºÑ Ðœ

我尝试使用 mb_convert_encoding、iconv 和 utf8_encode 将 DOMDocument 编码设置为不同的值(UTF-8、ISO-8859-1),但没有成功.

I've tried setting DOMDocument encoding to different values (UTF-8, ISO-8859-1), using mb_convert_encoding, iconv and utf8_encode but without success.

我怎样才能得到Алекс М"而不是ÐÐ»ÐµÐºÑ Ðœ"?

How can I get "Алекс М" instead of "ÐÐ»ÐµÐºÑ Ðœ" ?

输入来自加载了 curl 的页面.当我将页面内容输出到浏览器时,字符显示正确(所以我怀疑是输入的问题).

The input is coming from a page loaded with curl. When I output the page content to my browser, the characters are displayed correctly (so I doubt the input is the problem).

推荐答案

尝试:

$string = file_get_contents('your-xml-file.xml');
$string = mb_convert_encoding($string, 'utf-8', mb_detect_encoding($string));
// if you have not escaped entities use
$string = mb_convert_encoding($string, 'html-entities', 'utf-8'); 
$doc = new DOMDocument();
$doc->loadXML($string);

相关文章