在PHP中写入文本文件时,行中断不起作用。

时间:2022-10-17 16:55:51

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:

然而,当运行并打开usign记事本时,数据以一行的形式返回,没有间断如下:

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!

我找不到《疯狂盒子》的合适角色,但它确实是个疯狂的盒子。是什么给了!

4 个解决方案

#1


33  

If you want to open the file in Windows notepad, you must use Windows line breaks: \r\n

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

#2


52  

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;

PHP Constants

PHP的常量

#3


1  

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.

如果你在Windows上工作,可以使用Notepad2或notepad++。内置的记事本无法处理unix风格的行尾。

#4


0  

. PHP_EOL; will work universally

。PHP_EOL;将普遍

#1


33  

If you want to open the file in Windows notepad, you must use Windows line breaks: \r\n

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

#2


52  

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;

PHP Constants

PHP的常量

#3


1  

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.

如果你在Windows上工作,可以使用Notepad2或notepad++。内置的记事本无法处理unix风格的行尾。

#4


0  

. PHP_EOL; will work universally

。PHP_EOL;将普遍