php PHP换行符在文本文件中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4858443/
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
PHP newline not working in text file
提问by JJJollyjim
I am using the PHP code:
我正在使用 PHP 代码:
$numberNewline = $number . '\n';
fwrite($file, $numberNewline);
to write $number to a file.
将 $number 写入文件。
For some reason \n appears in the file. I am on a mac. What might be the problem?
出于某种原因,\n 出现在文件中。我在 mac 上。可能是什么问题?
回答by deceze
'\n'
in single quotes is a literal \n
."\n"
in double quotes is interpreted as a line break.
'\n'
在单引号中是一个文字\n
. "\n"
在双引号中被解释为换行符。
回答by borayeris
$numberNewline = $number . "\n";
fwrite($file, $numberNewline);
Try this
尝试这个
回答by Serge Stepanov
If inserting "\n" does not yield any results, you can also try "\r\n" which adds a "carriage-return" and "new line."
如果插入“\n”没有产生任何结果,您还可以尝试添加“回车”和“换行”的“\r\n”。
回答by vineet
Use PHP_EOL. PHP_EOL is platform-independent and good approach.
使用PHP_EOL。PHP_EOL 是独立于平台的好方法。
$numberNewline = $number .PHP_EOL;
fwrite($file, $numberNewline);
PHP_EOL is cross-platform-compatible(DOS/Mac/Unix).
PHP_EOL 是跨平台兼容的(DOS/Mac/Unix)。
回答by Jessica
The reason why you are not seeing a new line is because .txt files write its data like a stack. It starts writing from the beginning, then after it finishes, the blinking line (the one indicating where your next character is going to go) goes back to the beginning. So, your "\n" has to go in the beginning.
您没有看到新行的原因是 .txt 文件像堆栈一样写入其数据。它从头开始书写,然后在完成后,闪烁的线(指示下一个角色要去哪里的线)回到开头。所以,你的 "\n" 必须在开头。
Instead of writing:
而不是写:
<?php
$sampleLine = $variable . "\n";
$fwrite($file, $sampleLine);
?>
You should write:
你应该写:
<?php
$sampleLine = "\n" . $variable;
$fwrite($file, $sampleLine);
?>
回答by Karl Rapson
None of the above worked for me but it was so simple - here is the code... please use the KISS method.
以上都不适合我,但它是如此简单 - 这是代码......请使用 KISS 方法。
echo file_put_contents("test.txt","\r\n \r\n$name \r\n$email \r\n$phone", FILE_APPEND);
It set a new blank line and then appends one line at a time.
它设置一个新的空行,然后一次添加一行。
回答by ketan chaudhari
$numberNewline = $number . '\r\n';
fwrite($file, $numberNewline);
$numberNewline = $number 。'\r\n';
fwrite($file, $numberNewline);
Try This
尝试这个