在 PHP 中写入文本文件时,换行符不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4195986/
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
Line break not working when writing to text file in PHP
提问by JM4
I have the following test script:
我有以下测试脚本:
<?php
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Floppy Jalopy\n";
fwrite($fh, $stringData);
$stringData = "Pointy Pinto\n";
fwrite($fh, $stringData);
fclose($fh);
?>
when run however and opened usign Notepad, the data is returned in a single line without breaks as:
然而,当运行并打开使用记事本时,数据以单行形式返回,没有中断,如下所示:
Floppy Jalopy(crazy box)Pointy Pinto(crazy box)
Floppy Jalopy(crazy box)Pointy Pinto(crazy box)
where i cant find the appropriate character for 'crazy box' but its a REALLY crazy box. WHAT GIVES!
在那里我找不到“疯狂盒子”的合适字符,但它是一个非常疯狂的盒子。是什么赋予了!
回答by Evan Mulawski
It is best to use PHP_EOL
. This is cross-platform, so it automatically chooses the correct newline character(s) for the platform PHP is currently running on.
最好使用PHP_EOL
. 这是跨平台的,因此它会自动为 PHP 当前运行的平台选择正确的换行符。
$stringData = "Floppy Jalopy" . PHP_EOL;
回答by AndreKR
If you want to open the file in Windows notepad, you must use Windows line breaks: \r\n
如果要在 Windows 记事本中打开文件,必须使用 Windows 换行符: \r\n
回答by bcosca
回答by swarnim dixit
. PHP_EOL; will work universally
. PHP_EOL; 将普遍适用