在 PHP 中写入文本文件时换行符不起作用
我有以下测试脚本:
<?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);
?>
但是,当运行并使用记事本打开时,数据以单行形式返回,没有中断,如下所示:
Floppy Jalopy(疯狂盒子)Pointy Pinto(疯狂盒子)
,我找不到“疯狂盒子”的合适字符,但它确实是一个疯狂的盒子。有什么!
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)
where i cant find the appropriate character for 'crazy box' but its a REALLY crazy box. WHAT GIVES!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最好使用
PHP_EOL
。这是跨平台的,因此它会自动为 PHP 当前运行的平台选择正确的换行符。PHP 常量
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 Constants
如果要在 Windows 记事本中打开文件,则必须使用 Windows 换行符:
\r\n
If you want to open the file in Windows notepad, you must use Windows line breaks:
\r\n
你的代码运行良好。
使用 Notepad2 或 Notepad++(如果您使用的是 Windows)。内置记事本无法处理 Unix 风格的行结尾。
Your code runs fine.
Use Notepad2 or Notepad++ if you're working on Windows. The built-in Notepad is unable to cope with Unix-style line endings.
。 PHP_EOL;将普遍适用
. PHP_EOL; will work universally