PHP:每当我尝试编写 UTF-8 时,它都会使用 DOMDocument 写入它的十六进制表示法 [英] PHP: using DOMDocument whenever I try to write UTF-8 it writes the hexadecimal notation of it

查看:15
本文介绍了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.

例如:

ירושלים

代替:

ירושלים

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

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 写入它的十六进制表示法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆