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

查看:94
本文介绍了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加载到未指定编码的文档中,您将丢失在构造函数中声明的所有内容,这意味着:

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天全站免登陆