PHP中的UTF-8编码xml

时间:2022-10-24 23:35:14

I'm trying to output XML using PHP, and when I look at the page source in Firefox everything seems fine. However, the page itself doesn't display properly.

我正在尝试使用PHP输出XML,当我在Firefox中查看页面源时,一切似乎都很好。但是,页面本身无法正常显示。

In Firefox, it usually says this at the top of the page when it's showing correctly formatted XML:

在Firefox中,当它显示格式正确的XML时,它通常会在页面顶部显示:

This XML file does not appear to have any style information associated with it. The document tree is shown below.

..and then it shows the xml tree.

..然后它显示了xml树。

However, I just get the characters within the xml tags, and no tree.

但是,我只是获取xml标记中的字符,而没有树。

The only difference I can see is that my page is encoded as Western ISO-8859-1 and the correctly displayed XML is encoded as Unicode UTF-8. So how would I output my page as Unicode UTF8?

我能看到的唯一区别是我的页面编码为Western ISO-8859-1,正确显示的XML编码为Unicode UTF-8。那么如何将我的页面输出为Unicode UTF8?

I've tried this but it doesn't make any difference:

我试过这个,但它没有任何区别:

echo utf8_encode($xmlstring);

1 个解决方案

#1


9  

Make sure you send the XML with an appropriate content-type and encoding, e.g.

确保使用适当的内容类型和编码发送XML,例如

<?php header("Content-Type: application/xml; charset=utf-8"); ?>

Also check that the XML prolog contains the proper encoding, e.g.

还要检查XML prolog是否包含正确的编码,例如

<?xml version="1.0" encoding="UTF-8"?>

The message about the style information refers to a missing processing instruction, e.g.

关于样式信息的消息指的是丢失的处理指令,例如,

<?xml-stylesheet type="text/xsl" href="someting"?>

which would tell the browser how to style/format the output. It is not necessarily needed if you just want to display the raw XML.

这会告诉浏览器如何设置样式/格式化输出。如果您只想显示原始XML,则不一定需要它。

#1


9  

Make sure you send the XML with an appropriate content-type and encoding, e.g.

确保使用适当的内容类型和编码发送XML,例如

<?php header("Content-Type: application/xml; charset=utf-8"); ?>

Also check that the XML prolog contains the proper encoding, e.g.

还要检查XML prolog是否包含正确的编码,例如

<?xml version="1.0" encoding="UTF-8"?>

The message about the style information refers to a missing processing instruction, e.g.

关于样式信息的消息指的是丢失的处理指令,例如,

<?xml-stylesheet type="text/xsl" href="someting"?>

which would tell the browser how to style/format the output. It is not necessarily needed if you just want to display the raw XML.

这会告诉浏览器如何设置样式/格式化输出。如果您只想显示原始XML,则不一定需要它。