使用DOMDocument进行PHP编码

Oli*_*nde 23 php dom character-encoding

<tag>
????? ?
</tag>
Run Code Online (Sandbox Code Playgroud)

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

ÐÐ»ÐµÐºÑ Ðœ
Run Code Online (Sandbox Code Playgroud)

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

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

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

Dmy*_*kin 42

尝试:

$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);
Run Code Online (Sandbox Code Playgroud)


Nem*_*mke 19

在使用XPath解析DomDocument之后,我看到了类似的问题

https://bugs.php.net/bug.php?id=32547

我这样解决了

// Workaround because PHP 5.2.x has encoding problems, when we 
// update to PHP 5.3 this line is not necesserry any more
$content = '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />' . $content;

// Creating new DOM document and loading HTML content
$dom_document = new DOMDocument('1.0', 'UTF-8');
$dom_document->substituteEntities = TRUE;
$dom_document->loadHTML($content);
Run Code Online (Sandbox Code Playgroud)

  • 感谢您发布此内容.我正在维护运行PHP 5.2.6的旧服务器,并且一直有这个问题.这解决了它. (2认同)

Cas*_*sey 6

将xml标头添加到标签 - 试试这个:

$a = new DOMDocument ();
$a->loadXml ('<?xml version="1.0" encoding="UTF-8"?><tag>????? ?</tag>');
print htmlspecialchars ($a->saveXml ());
Run Code Online (Sandbox Code Playgroud)