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


php create text file. Linebreaks are wrong

我使用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;

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

如果我在记事本++中打开文件,我确实会在"回车"处得到换行符在普通的记事本上,甚至不存在中断。下载的文本文件中的输出在引号之间如下:

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

所以。我做错了什么?在标头之前输出某些内容,因此标头不起作用?

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

输出开头的两个空格的问题与给定示例中的代码.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;