<bdo id='nuLcA'></bdo><ul id='nuLcA'></ul>
    <legend id='nuLcA'><style id='nuLcA'><dir id='nuLcA'><q id='nuLcA'></q></dir></style></legend>

    <small id='nuLcA'></small><noframes id='nuLcA'>

    1. <i id='nuLcA'><tr id='nuLcA'><dt id='nuLcA'><q id='nuLcA'><span id='nuLcA'><b id='nuLcA'><form id='nuLcA'><ins id='nuLcA'></ins><ul id='nuLcA'></ul><sub id='nuLcA'></sub></form><legend id='nuLcA'></legend><bdo id='nuLcA'><pre id='nuLcA'><center id='nuLcA'></center></pre></bdo></b><th id='nuLcA'></th></span></q></dt></tr></i><div id='nuLcA'><tfoot id='nuLcA'></tfoot><dl id='nuLcA'><fieldset id='nuLcA'></fieldset></dl></div>

      <tfoot id='nuLcA'></tfoot>
    2. PHP:每当我尝试编写 UTF-8 时,它都会使用 DOMDocu

      时间:2023-10-03

      <i id='Qb7KJ'><tr id='Qb7KJ'><dt id='Qb7KJ'><q id='Qb7KJ'><span id='Qb7KJ'><b id='Qb7KJ'><form id='Qb7KJ'><ins id='Qb7KJ'></ins><ul id='Qb7KJ'></ul><sub id='Qb7KJ'></sub></form><legend id='Qb7KJ'></legend><bdo id='Qb7KJ'><pre id='Qb7KJ'><center id='Qb7KJ'></center></pre></bdo></b><th id='Qb7KJ'></th></span></q></dt></tr></i><div id='Qb7KJ'><tfoot id='Qb7KJ'></tfoot><dl id='Qb7KJ'><fieldset id='Qb7KJ'></fieldset></dl></div>
        <tfoot id='Qb7KJ'></tfoot>
            <tbody id='Qb7KJ'></tbody>

              <legend id='Qb7KJ'><style id='Qb7KJ'><dir id='Qb7KJ'><q id='Qb7KJ'></q></dir></style></legend>

            1. <small id='Qb7KJ'></small><noframes id='Qb7KJ'>

                <bdo id='Qb7KJ'></bdo><ul id='Qb7KJ'></ul>
                本文介绍了PHP:每当我尝试编写 UTF-8 时,它都会使用 DOMDocument 写入它的十六进制表示法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                当我尝试使用 DOMDocument 将 UTF-8 字符串写入 XML 文件时,它实际上写入的是字符串的十六进制表示法,而不是字符串本身.

                When I try to write UTF-8 Strings into an XML file using DOMDocument it actually writes the hexadecimal notation of the string instead of the string itself.

                例如:

                &#x5D9;&#x5E8;&#x5D5;&#x5E9;&#x5DC;&#x5D9;&#x5DD;
                

                代替:

                ירושלים
                

                有什么想法可以解决这个问题吗?

                Any ideas how to resolve the issue?

                推荐答案

                好的,给你:

                $dom = new DOMDocument('1.0', 'utf-8');
                $dom->appendChild($dom->createElement('root'));
                $dom->documentElement->appendChild(new DOMText('ירושלים'));
                echo $dom->saveXml();
                

                会正常工作,因为在这种情况下,您构建的文档将保留指定为第二个参数的编码:

                will work fine, because in this case, the document you constructed will retain the encoding specified as the second argument:

                <?xml version="1.0" encoding="utf-8"?>
                <root>ירושלים</root>
                

                但是,一旦将 XML 加载到未指定编码的 Document 中,您将丢失在构造函数中声明的任何内容,这意味着:

                However, once you load XML into a Document that does not specify an encoding, you will lose anything you declared in the constructor, which means:

                $dom = new DOMDocument('1.0', 'utf-8');
                $dom->loadXml('<root/>'); // missing prolog
                $dom->documentElement->appendChild(new DOMText('ירושלים'));
                echo $dom->saveXml();
                

                不会有 utf-8 编码:

                will not have an encoding of utf-8:

                <?xml version="1.0"?>
                <root>&#x5D9;&#x5E8;&#x5D5;&#x5E9;&#x5DC;&#x5D9;&#x5DD;</root>
                

                因此,如果您加载 XML 内容,请确保它是

                So if you loadXML something, make sure it is

                $dom = new DOMDocument();
                $dom->loadXml('<?xml version="1.0" encoding="utf-8"?><root/>');
                $dom->documentElement->appendChild(new DOMText('ירושלים'));
                echo $dom->saveXml();
                

                它会按预期工作.

                作为替代,您也可以指定编码 加载文档后.

                As an alternative, you can also specify the encoding after loading the document.

                这篇关于PHP:每当我尝试编写 UTF-8 时,它都会使用 DOMDocument 写入它的十六进制表示法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:PHP 替换特殊字符,如 à->a、è->e 下一篇:带有 utf-8 的 php substr() 函数在末尾留下 标记

                相关文章

                最新文章

              • <i id='3JXkf'><tr id='3JXkf'><dt id='3JXkf'><q id='3JXkf'><span id='3JXkf'><b id='3JXkf'><form id='3JXkf'><ins id='3JXkf'></ins><ul id='3JXkf'></ul><sub id='3JXkf'></sub></form><legend id='3JXkf'></legend><bdo id='3JXkf'><pre id='3JXkf'><center id='3JXkf'></center></pre></bdo></b><th id='3JXkf'></th></span></q></dt></tr></i><div id='3JXkf'><tfoot id='3JXkf'></tfoot><dl id='3JXkf'><fieldset id='3JXkf'></fieldset></dl></div>

                <tfoot id='3JXkf'></tfoot>

                    <bdo id='3JXkf'></bdo><ul id='3JXkf'></ul>
                1. <legend id='3JXkf'><style id='3JXkf'><dir id='3JXkf'><q id='3JXkf'></q></dir></style></legend>

                    <small id='3JXkf'></small><noframes id='3JXkf'>