PHP:每当我尝试编写 UTF-8 时,它都会使用 DOMDocument 写入它的十六进制表示法

2021-12-28 00:00:00 utf-8 php domdocument hebrew

当我尝试使用 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.

例如:

ירושלים

代替:

ירושלים

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

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.

相关文章