问题
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>
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();
}
and i get this:
<?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?
translates to:
¿Dónde habré ; olvidado... mi memoria?
I need original chars in the XML, these are utf-8 valid and i don't know the reason for this encode :(
回答1:
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();
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();
回答2:
I solved with this:
header('Content-type: text/html; charset=utf-8');
if(isset($_POST) && isset($_POST['data']))
{
$data = '<?xml version="1.0" encoding="utf-8"?>';
$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();
adding the:
$data = '<?xml version="1.0" encoding="utf-8"?>';
to the XML at the beginning
thanks for responses :)
来源:https://stackoverflow.com/questions/4625173/php-xml-dom-translates-special-chars-to-xyy