php XML DOM 将特殊字符转换为 &#xYY;

发布于 2024-10-10 19:49:32 字数 1638 浏览 6 评论 0原文

我用 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:

        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': &#xBF;D&#xF3;nde habr&#xE9; olvidado... mi memoria?</span></div>    </li></ul></li></ul></li></ul>

¿Dónde habré olvidado...mi memoria?

翻译为:

&#xBF;D&#xF3;nde habr&#xE9 ; olvidado... mi memoria?

我需要 XML 中的原始字符,这些字符是 utf-8 有效的,我不知道这种编码的原因:(

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 :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月下客 2024-10-17 19:49:32

解决此问题的最简单方法是在加载 XML 之后设置编码类型:

$dom = new DOMDocument();
$dom->loadXML($data);
$dom->encoding = 'utf-8';

echo $dom->saveXML();                       
exit();

您还可以通过在数据开头放置 XML 声明来解决此问题:

$data = '<?xml version="1.0" encoding="utf-8"?>' . $data;
$dom = new DOMDocument();
$dom->loadXML($data);

echo $dom->saveXML();
exit();

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();
街角卖回忆 2024-10-17 19:49:32

我解决了这个问题:在

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

开头添加:

            $data = '<?xml version="1.0" encoding="utf-8"?>';

到 XML

感谢您的回复:)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文