如何在创建的文本文件上添加新行?(php)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12880094/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:25:30  来源:igfitidea点击:

how to add new line on an created text file ?(php)

php

提问by Tarek Siddiki

i am working on a php script. it is as follows:

我正在编写一个 php 脚本。如下:

<?
$a='a b c d e';
$b=explode(" ",$a);
foreach ($b as $c) {

$filename = 'test.txt';
$fp = fopen($filename, "a+");
$write = fputs($fp, $c."/n");
fclose($fp);

}
?>

the output (test.txt) comes as this:

输出(test.txt)如下:

a/nb/nc/nd/ne/n

But I want to get the text file like this:

但我想得到这样的文本文件:

a
b
c
d
e

can any body tell me how to do this?

任何人都可以告诉我该怎么做吗?

回答by HorusKol

Escape sequences are indicated with a \not /- so you need to use "\n" to put a newline into your string (and file)

转义序列用\not表示/- 因此您需要使用“\n”将换行符放入字符串(和文件)中

回答by user3186859

on line 8 you can add \n\r

在第 8 行,您可以添加 \n\r

This will make it cross platform from Windows/MAC/LINUX/UNIX.

这将使它从 Windows/MAC/LINUX/UNIX 跨平台。

eg. $write = fputs($fp, $c."\r\n");

例如。$write = fputs($fp, $c."\r\n");