文本文件中 PHP 输出中的新行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12908334/
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
New line in PHP output in a text file
提问by Darth Severus
My php form which saves the output in a text file glues the result to one string like:
我的 php 表单将输出保存在文本文件中,将结果粘贴到一个字符串中,例如:
Name1Email1The Message1Name2Email2The Message2Name3Email3The Message3Name4Email4The Message4
名称1电子邮件1消息1名称2电子邮件2消息2名称3电子邮件3消息3名称4电子邮件4消息4
But I need spaces and/or newlines. I normaly don't use PHP so I don't get it. I did't find an answer on the web, also read some Q/A here, but this didn't help me.
但我需要空格和/或换行符。我通常不使用 PHP,所以我不明白。我没有在网上找到答案,也在这里阅读了一些问答,但这对我没有帮助。
The Form:
表格:
<form action="mailer.php?savedata=1" method="post">
Your Name: <input type="text" name="name"><br>
Your Email: <input type="text" name="email"><br>
Your Message:<br> <textarea name="message" rows="5" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
The PHP
PHP
<?php
$savedata = $_REQUEST['savedata'];
if ($savedata == 1){
$data = $_POST['name'];
$data .= $_POST['email'];
$data .= $_POST['message'];
$file = "YOURDATAFILE.txt";
$fp = fopen($file, "a") or die("Couldn't open $file for writing!");
fwrite($fp, $data) or die("Couldn't write values to file!");
fclose($fp);
echo "Your Form has been Submitted!";
}
?>
回答by Dallin
As other answers state, you need to add in an end of line character after each field.
正如其他答案所述,您需要在每个字段后添加行尾字符。
Different OS's use different line endings, though, and so a "\n" may not display as a new line on Windows, for example. As Mahdi said, you can use Windows style "\r\n" line endings, or you can use the PHP_EOLconstant so that line endings appropriate to the server will be output, in which case your code would look like
但是,不同的操作系统使用不同的行结尾,因此“\n”可能不会在 Windows 上显示为新行,例如。正如 Mahdi 所说,您可以使用 Windows 样式的 "\r\n" 行尾,或者您可以使用PHP_EOL常量,以便输出适合服务器的行尾,在这种情况下,您的代码将如下所示
$data = $_POST['name'] . PHP_EOL;
$data .= $_POST['email'] . PHP_EOL;
$data .= $_POST['message'] . PHP_EOL;
回答by Louis Huppenbauer
You can use "\n" to write a new line.
您可以使用“\n”来写一个新行。
$data = $_POST['name'] . "\n";
$data .= $_POST['email'] . "\n";
$data .= $_POST['message'] . "\n";
Just as a sidenote, \n has to be in doublequotes. When surrounded by single quotes it won't work.
就像旁注一样,\n 必须用双引号引起来。当被单引号包围时,它将不起作用。
回答by Ja?ck
When you concatenate your input values like that you won't have any newlines; you can add them using the literal "\n"or "\r\n". To string them all together you can use the concatenation operator or use sprintf()like below:
当您像这样连接输入值时,您将不会有任何换行符;您可以使用文字"\n"或"\r\n". 要将它们串在一起,您可以使用连接运算符或sprintf()如下使用:
$data = sprintf("%s\n%s\n%s\n", $_POST['name'], $_POST['email'], $_POST['messge']);
$file = "YOURDATAFILE.txt";
file_put_contents($file, $data, FILE_APPEND);
I also use file_put_contents()with the FILE_APPENDflag to append data to a file and save a few more lines of code.
我还使用file_put_contents()该FILE_APPEND标志将数据附加到文件并保存多行代码。
回答by Mahdi
I usually combine New Linewith Carriage Return:
我通常结合New Line有Carriage Return:
$data = $_POST['name'] . "\r\n";
回答by Greg Adams
Check the filter that you're using to clean form input. It may be removing the 0x0a NewLine characters from your form input.
检查您用来清理表单输入的过滤器。它可能会从您的表单输入中删除 0x0a NewLine 字符。
I have text saved from a 'textarea' tag and it's correct. Using a HEX viewer, I see that each newline from the "textarea" field gets translated to hex character 0a. (decimal 10) Wikipedia lists this as the UTF-8 "newline character" or "LF." LF was short for Line Feed, from the old teletype days. https://en.wikipedia.org/wiki/UTF-8
我从“textarea”标签中保存了文本,这是正确的。使用十六进制查看器,我看到“textarea”字段中的每个换行符都被转换为十六进制字符 0a。(十进制 10)维基百科将其列为 UTF-8“换行符”或“LF”。LF 是 Line Feed 的缩写,来自旧的电传时代。 https://en.wikipedia.org/wiki/UTF-8
So, the lines test test test translate to "4 65 73 74 0d 0a 74 65 73 74 0d 0a 74 65 73 74" Note the two "0a" characters in the string, which are equivalent to \n.
因此,行 test test test 转换为“4 65 73 74 0d 0a 74 65 73 74 0d 0a 74 65 73 74” 注意字符串中的两个“0a”字符,它们等价于\n。
This wasn't apparent at first because the regular expression I was using to clean the input was replacing the 0a character with a space.
这起初并不明显,因为我用来清理输入的正则表达式正在用空格替换 0a 字符。
回答by May Noppadol
this works for me:
这对我有用:
echo "$data"."\r\n";
回答by DiverseAndRemote.com
you'r $datavariable needs to already have the \nnew line characters in it.
您的$data变量需要已经包含\n新行字符。
回答by user2396877
When we attempt to extract data submitted online via a Web-Form and to write the same to a text file which is placed on the web-server -- without overwriting the information already contained in the said text file, then we use the following code within the tags:
当我们尝试提取通过 Web 表单在线提交的数据并将其写入放置在 Web 服务器上的文本文件时——不覆盖已包含在所述文本文件中的信息,然后我们使用以下代码在标签内:
<html>
<body>
<form action="" method="">
<input value="submit">
<div>Name: <input value=""></div>
<div>email: <input value=""></div>
... ... ...
<div><button>Submit</button></div>
</form>
</body>
</html>
The above HTML Code is coupled with the following PHP Code:
上面的 HTML 代码加上下面的 PHP 代码:
<?php
$fileName = 'fdata.txt';
$fp = fopen('fdata.txt', 'a');
$savestring = $_POST['name'] . ',' . $_POST['email'] ;
fwrite($fp, $savestring);
fclose($fp);
?>
But the above two Codes together lead to data that is appended and stringed together in the text file -- that is to say, every new record (set of data) extracted from subsequent Web-Forms does not begin at a new line (there is no line-break or carriage-return).
但是上面的两个 Code 一起导致数据在文本文件中被追加和串在一起——也就是说,从后续 Web-Forms 中提取的每条新记录(数据集)都不会从新行开始(有没有换行符或回车符)。
I searched the Net for a Solution to the above Issue -- as suggested on the Net, I added “\r\n” (in double quotes) and also used ‘PHP_EOL' (without quotes) as under:
我在网上搜索了上述问题的解决方案——正如网上所建议的,我添加了“\r\n”(双引号)并使用了“PHP_EOL”(不带引号),如下所示:
$fp = fopen('fdata.txt', 'a', "\r\n");
$savestring = “\r\n” . PHP_EOL . ' ' . $_POST['name'] . ',' . $_POST['email'] .
PHP_EOL;
Yet there was no line-break at all (my web-server and my desktop use Windows). It means that there was no practicable Solution within the knowledge of anyone of the millions of Users of Net in the World. It appears that the parameter ‘a' overrides “\r\n”, ‘PHP_EOL' etc.
然而根本没有换行(我的网络服务器和我的桌面使用 Windows)。这意味着在世界上数百万网络用户的知识范围内,没有任何可行的解决方案。似乎参数“a”覆盖了“\r\n”、“PHP_EOL”等。

