php 创建文本文件。换行符是错误的

标签 php file text download line-breaks

我使用 php 创建了一个可以在按下链接时下载的文件。

已经尝试了多种代码变体。没有理由这个不应该工作:

$output = 'test    spaces, test 
enter, test n, \ntest nr, \n\rtest nt, \n\ttest html-br, <br /><br>end of line';
$output .= '\r\n';
$output .= 'blahblah';
header('Content-type: text/plain');
header('Content-Disposition: attachment;filename="'.$filename.'.txt"');
print $output;
exit;

但是返回的文件以我没有插入的两个空格开头。\r\n 上没有换行符。

如果我在 notepad++ 中打开文件,我会在“enter”上得到一个换行符 在普通的记事本中,中断甚至不存在。下载的文本文件中的输出在引号之间是这样的:

  In notepad++
  "  test    spaces, test 
  enter, test n, \ntest nr, \n\rtest nt, \n\ttest html-br, <br /><br>end of line\r\nblahblah"

所以。我究竟做错了什么?在 header 之前输出一些东西,所以 header 不起作用?

更新:感谢您的帮助。两个正确答案同时出现。订购“最老的第一”时的第一个答案被标记为正确答案。使用单引号解决了这个问题。

输出开头的两个空格问题与给定示例中的 code/php 无关。它是所用框架的产物。

最佳答案

换行符(使用 \n\r)只能在双引号中识别,不能在单引号中识别。

关于php 创建文本文件。换行符是错误的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7202570/

相关文章:

java - 在 java 中读取文本文件——为什么会跳行?

text - 使用 tk 创建文本编辑器

html - 如何在 CSS 中将跨度文本居中放置在图像旁边

php - 尝试使用正则表达式从 php 中的 "suburb name"中提取 "12 street name, suburb name, CTG 1234"

php - 如何在 php 中从数据库中获取新记录的数量?

php - 通过复选框选择通过 PHP 将 MySQL 表导出到 CSV

php - 组合 : avoiding multiple nested foreach

c++ - 在c++目录中查找*.txt文件

c - 如何将文件中的数据插入到队列的节点中?

shell - 无法将进程的 Unix ID 转换为文本?