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

php创建文本文件。换行符是错误的,php,file,text,download,line-breaks,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/p

我使用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;
$output='测试空间,测试
输入,测试编号,\n测试编号,\n\r测试编号,\n\t测试html br,

行末'; $output.='\r\n'; $output.='blahblah'; 标题(“内容类型:文本/普通”); 标题('Content-Disposition:attachment;filename=“.”.$filename..txt“); 打印$输出; 出口
但是返回的文件以我没有插入的两个空格开始。上没有换行符\r\n

如果我用记事本++打开文件,我会在“回车”键上得到一个换行符 在普通的记事本上,那个破绽是不均匀的。下载文本文件中的输出如下所示:

  In notepad++
  "  test    spaces, test 
  enter, test n, \ntest nr, \n\rtest nt, \n\ttest html-br, <br /><br>end of line\r\nblahblah"
记事本中的
++
“测试空间,测试
输入,测试n,\n测试编号,\n\r测试nt,\n\t测试html br,

行尾\r\nblahblah“
所以。我做错了什么?在标头之前输出某些内容,因此标头不工作

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


输出开头两个空格的问题与示例中的code/php无关。它是所用框架中的一个工件。

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

请改用双引号,单引号不能识别新行

// Outputs: This will not expand: \n a newline
echo 'This will not expand: \n a newline';

// Outputs: Variables do not $expand $either
echo 'Variables do not $expand $either';
所以看起来像

$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;
$output=“测试空间,测试
输入,测试编号,\n测试编号,\n\r测试编号,\n\t测试html br,

行尾; $output.=“\r\n”; $output.='blahblah'; 标题(“内容类型:文本/普通”); 标题('Content-Disposition:attachment;filename=“.”.$filename..txt“); 打印$输出; 出口
谢谢。完全正确新建我需要找到行开头的两个换行符的来源。您在某些地方使用
\n\r
,这可能会导致双换行符,具体取决于操作系统。双引号。这就是我最后45分钟的消磨时间。双引号。非常感谢。这是一个多么简单的细节,却有着巨大的差异。