本文介绍了php XML DOM 将特殊字符转换为 &#xYY;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用 AJAX POST 发送:
I send this with AJAX POST:
<li><ul class "zone zCentral ui-sortable"><li><ul class="region rCol3 ui-sortable"><li class="" style=""><div><span class="tc tc_video">574081</span> <span>video: 'Mundo.Hoy': ¿Dónde habré olvidado... mi memoria?</span></div></li></ul></li></ul></li>
我这样做是为了创建 XML:
I do this to create XML:
header('Content-type: text/html; charset=utf-8');
if(isset($_POST) && isset($_POST['data']))
{
$data = '<ul id="zone_container" class="ui-sortable">';
$data .= $_POST['data'];
$data .= '</ul>';
$dom = new DOMDocument('1.0', 'utf-8');
$dom->loadXML($data);
echo $dom->saveXML();
exit();
}
我明白了:
<?xml version="1.0"?>
<ul id="zone_container" class="ui-sortable">
<li><ul class="zone zCentral ui-sortable"><li><ul class="region rCol3 ui-sortable"><li class="" style=""><div><span class="tc tc_video">574081</span> <span>video: 'Mundo.Hoy': ¿Dónde habré olvidado... mi memoria?</span></div> </li></ul></li></ul></li></ul>
¿Dónde habré olvidado... mi memoria?
¿Dónde habré olvidado... mi memoria?
翻译成:
¿Dónde habré ; olvidado... mi memoria?
我需要 XML 中的原始字符,这些是 utf-8 有效的,我不知道这种编码的原因:(
I need original chars in the XML, these are utf-8 valid and i don't know the reason for this encode :(
推荐答案
解决此问题的最简单方法是在加载 XML 之后设置编码类型:
The easiest way to fix this is to set the encoding type after you have loaded the XML:
$dom = new DOMDocument();
$dom->loadXML($data);
$dom->encoding = 'utf-8';
echo $dom->saveXML();
exit();
您也可以通过在数据的开头放置 XML 声明来修复它:
You can also fix it by putting an XML declaration at the beginning of your data:
$data = '<?xml version="1.0" encoding="utf-8"?>' . $data;
$dom = new DOMDocument();
$dom->loadXML($data);
echo $dom->saveXML();
exit();
这篇关于php XML DOM 将特殊字符转换为 &amp;#xYY;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!