PHP encoding with DOMDocument

别等时光非礼了梦想. 提交于 2019-11-27 11:49:40

Try:

$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);

I had a similar problem after using XPath to parse DomDocument, and after reading this

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

I solved it like this

// 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);

Add xml header to you tags - try this:

$a = new DOMDocument ();
$a->loadXml ('<?xml version="1.0" encoding="UTF-8"?><tag>Алекс М</tag>');
print htmlspecialchars ($a->saveXml ());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!