php PHP中的UTF-8编码xml

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4164719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:10:32  来源:igfitidea点击:

UTF-8 encoding xml in PHP

phpxmlutf-8

提问by cannyboy

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?

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

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

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

echo utf8_encode($xmlstring);

回答by Gordon

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,则不一定需要它。