文档末尾的额外内容 PHP 和 XML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8850120/
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-26 05:38:10  来源:igfitidea点击:

Extra content at the end of the document PHP and XML

phpxml

提问by Jeff

I having a strange problem when I'm trying to produce an XML file with PHP. The problems is that, a strange question mark appears at the end of the source code. And I get the error: "Extra content at the end of the document"

我在尝试用 PHP 生成 XML 文件时遇到了一个奇怪的问题。问题是,在源代码的末尾出现了一个奇怪的问号。我收到错误消息:“文档末尾有额外的内容”

I'm running this script:

我正在运行这个脚本:

<?php
header("Content-Type: application/xml");
echo "<?xml version=\"1.0\"?>";
?>

<Module>
      <ModulePrefs title="Ngram Extractor"
        author="interedition team"
       description="Ngram Extractor"
        scrolling="true"/>
        <Content type="html">
        Test
        </Content>
</Module>?

When I open this is a browser, I get the state error and the source code looks like this. Notice the strange question mark at the end.

当我打开这是浏览器时,出现状态错误,源代码如下所示。注意结尾处奇怪的问号。

 <?xml version="1.0"?>
<Module>
      <ModulePrefs title="Ngram Extractor"
        author="interedition team"
       description="Ngram Extractor"
        scrolling="true"/>
        <Content type="html">
        Test
        </Content>
</Module>?

Please help.

请帮忙。

回答by Jpsy

You have a zero width spacecharacter at the end of your file - UTF-8 code E2 80 8B. It is placed right after </Module>. Have a look at your file in hex mode. This extra character prevents your browser from recognizing this as valid XML and - depending on the browser in use - shows up as a question mark or does not show up at all.

您的文件末尾有一个零宽度空格字符 - UTF-8 代码 E2 80 8B。它紧跟在 之后</Module>。以十六进制模式查看您的文件。这个额外的字符会阻止您的浏览器将其识别为有效的 XML,并且 - 根据使用的浏览器 - 显示为问号或根本不显示。

Remove that extra character and you will be fine.

删除那个额外的字符,你会没事的。

回答by Ramil Amerzyanov

May be you use UTF-8 with BOM encoding. Check it

可能是您将 UTF-8 与 BOM 编码一起使用。核实

回答by Jeff

I don't really have answer, accept that my editor wasn't showing the question mark. But one I opened the file in notepad2, lo and behold, the question mark was there. Very strange. Thanks for your help.

我真的没有答案,接受我的编辑没有显示问号。但是我在notepad2中打开了文件,瞧,问号就在那里。很奇怪。谢谢你的帮助。