30

我有以下测试脚本:

<?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);

?>

但是,当运行并打开 usign 记事本时,数据将在一行中返回,没有中断,如下所示:

Floppy Jalopy(疯狂盒子)Pointy Pinto(疯狂盒子)

我找不到适合“疯狂盒子”的角色,但它是一个非常疯狂的盒子。是什么赋予了!

4

4 回答 4

65

最好使用PHP_EOL. 这是跨平台的,因此它会自动为 PHP 当前运行的平台选择正确的换行符。

$stringData = "Floppy Jalopy" . PHP_EOL;

PHP 常量

于 2010-11-16T16:00:23.650 回答
36

如果要在 Windows 记事本中打开文件,则必须使用 Windows 换行符:\r\n

于 2010-11-16T15:56:18.700 回答
1

您的代码运行良好。

如果您在 Windows 上工作,请使用Notepad2Notepad++ 。内置记事本无法处理 Unix 风格的行尾。

于 2010-11-16T16:11:37.440 回答
0

. PHP_EOL;将普遍工作

于 2017-05-11T08:21:28.583 回答