Apache 或 PHP 生成前置换行符

编程入门 行业动态 更新时间:2024-10-28 02:26:22
本文介绍了Apache 或 PHP 生成前置换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试在 PHP Web 应用程序中生成 XML 文件:

I am trying to generate an XML file in a PHP web application:

<?php
... 
header('Content-Type: application/xml');
header('Content-Disposition: attachment; filename=test.xml');
echo "<?xml version=\"1.0\"?>\r\n" . 
...

奇怪的是,当使用我的服务器时(分别是 PHP 版本 5.3.8/Apache 2.2.17 和 PHP 版本 5.3.10-1/Apache 2.2.22)换行(十六进制 0a) 字符插入到输出的开头,导致无法使用的无效 XML.还有 在线关于这个问题,未解决.

Bizarrely, when using my servers (PHP Version 5.3.8/Apache 2.2.17 and PHP Version 5.3.10-1/Apache 2.2.22 respectively) a line feed (hex 0a) character is inserted in the beginning of the output, resulting in invalid XML that cannot be used. There's one more online question about this, unresolved.

所以如果我尝试 echo "bug"; 我得到 4 个字节,而不是 3 个:0a 62 75 67

So if I try echo "bug"; I get 4 bytes, not 3: 0a 62 75 67

但是,在本地使用 WAMP 服务器(PHP 5.4.3/Apache 2.4.2)时,我得到 3 个字节:62 75 67.

However, when using WAMP server locally (PHP 5.4.3/Apache 2.4.2), I get 3 bytes: 62 75 67.

这是已知错误/功能吗?这是配置问题吗?应该归咎于 Apache 还是PHP?我需要升级我的服务器吗?我宁愿不要.

推荐答案

0a 问题似乎是由我的主要 PHP 文件中包含的 PHP 文件中的尾随 Enter 字符引起的.当我从包含文件中删除它时,输出中的 0a 字符消失了.

It seems like the 0a problem was caused by a trailing Enter character in a PHP file included by my main PHP file. When I removed it from the include file, the 0a character in my output disappeared.

我觉得这件事的奇特之处在于我经历过的 PHP 版本之间对空格的不同处理,以及在测试社区的建议时我仍然得到 0a 的事实.

What I find peculiar about this is the different handling of whitespace between PHP versions that I experienced, and the fact that I still got the 0a when testing the community's suggestions.

我没有更多时间对此进行研究,但我对遇到类似问题的人的建议是检查包含文件中的空格是否可能会影响等式.此外,避免按照下面 Dan 的建议结束 <?php 标签.

I have no more time to put research into this, but my advice to people experiencing similar problems is to check whether whitespace in include files may play into the equation. In addition, avoid ending the <?php tag as suggested by Dan below.

这篇关于Apache 或 PHP 生成前置换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-20 00:26:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/972351.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:换行符   Apache   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!