php file_put_contents、file_append 和换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3536564/
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
file_put_contents, file_append and line breaks
提问by user426965
I'm writing a PHP script that adds numbers into a text file. I want to have one number on every line, like this:
我正在编写一个将数字添加到文本文件中的 PHP 脚本。我希望每一行都有一个数字,如下所示:
1
5
8
12
If I use file_put_contents($filename, $commentnumber, FILE_APPEND)
, the result looks like:
如果我使用file_put_contents($filename, $commentnumber, FILE_APPEND)
,结果如下:
15812
If I add a line break like file_put_contents($filename, $commentnumber . "\n", FILE_APPEND)
, spaces are added after each number and one empty line at the end (underscore represents spaces):
如果我添加一个换行符file_put_contents($filename, $commentnumber . "\n", FILE_APPEND)
,则在每个数字后添加空格,并在末尾添加一个空行(下划线代表空格):
1_
5_
8_
12_
_
_
How do I get that function to add the numbers the way I want, without spaces?
我如何让该函数以我想要的方式添加数字,没有空格?
回答by eridal
Did you tried with PHP EOL constant?
您是否尝试过使用 PHP EOL 常量?
file_put_contents($filename, $commentnumber . PHP_EOL, FILE_APPEND)
--- Added ---
- - 添加 - -
I just realize that my file editor does the same, but don't worrie, is just a ghost character that the editor places there to signal that there is a newline You could try this
我只是意识到我的文件编辑器做了同样的事情,但不要担心,只是编辑器放置在那里的一个幽灵字符,表示有一个换行符你可以试试这个
A file with EOL after the last number looks like: 1_ 2_ 3_ EOF but a file without that last character looks like 1_ 2_ 3 EOF where _ means a space character
You could try to parse the file contents using php to see what's inside
您可以尝试使用 php 解析文件内容以查看其中的内容
$lines = explode( PHP_EOL, file_get_contents($file));
foreach($lines as $line ) {
var_dump($line);
}
...tricky
...棘手
回答by The Surrican
pauls answer has the correct approach but he has a mistake. what you need ist the following:
pauls answer 有正确的方法,但他有一个错误。您需要的是以下内容:
file_put_contents($filename, trim($commentnumber).PHP_EOL, FILE_APPEND);
the PHP_EOL constant makes sure to use the right line ending on mac, windows and unix systems the trim function removes any newline or whitespace on both sides of the string. converting to integer would be a huge mistake because 1. you might end up having zero, expecially because of white space or special characters (wherever they come from...) 2. ids dont necessarily need to be integers
PHP_EOL 常量确保在 mac、windows 和 unix 系统上使用正确的行结尾,trim 函数删除字符串两侧的任何换行符或空格。转换为整数将是一个巨大的错误,因为 1. 你最终可能会得到零,特别是因为空格或特殊字符(无论它们来自哪里......) 2. id 不一定需要是整数
回答by M_R_K
Ohh Guys! Just Use
哦,伙计们!就用
\r\n
\r\n
insted of \n
插入\n
回答by Timwi
There is nothing in the code you provided that would generate those spaces, unless $commentnumber
already contains the space to begin with. If that is the case, simply use trim($commentnumber)
instead.
您提供的代码中没有任何内容可以生成这些空格,除非$commentnumber
已经包含开头的空格。如果是这种情况,只需trim($commentnumber)
改用即可。
There is also nothing in your code that would explain empty lines at the bottom of the file, unless $commentnumber
can be an empty string. If that is the case, and you want it to output the number 0
instead, use intval($commentnumber)
.
您的代码中也没有任何内容可以解释文件底部的空行,除非$commentnumber
可以是空字符串。如果是这种情况,并且您希望它改为输出数字0
,请使用intval($commentnumber)
.
Of course, you need only one of those two. If you want to preserve string-like content, use trim()
; if you always want integers, use intval()
, which already trims it automatically.
当然,您只需要这两者之一。如果要保留类似字符串的内容,请使用trim()
; 如果您总是想要整数,请使用intval()
,它已经自动修剪了它。
It is also possible that you accidentally wrote " \n"
instead of "\n"
in your actual code, but in the code you posted here it is correct.
也有可能您不小心编写了" \n"
而不是"\n"
在您的实际代码中,但在您在这里发布的代码中它是正确的。
回答by Paul Dragoonis
annoyingregistration, what you have there is absolutely fine. PHP_EOL and "\n" are exactly the same.
烦人的注册,您拥有的绝对没问题。PHP_EOL 和 "\n" 完全一样。
The code you provided theres nothing wrong with it so it must be the value of $commentnumber that has a space at the end of it. as stated, run your $commentnumber through the trim() function.
你提供的代码没有任何问题,所以它必须是 $commentnumber 的值,在它的末尾有一个空格。如上所述,通过trim()函数运行您的$commentnumber 。
file_put_contents($filename, trim($commentnumber . "\n"), FILE_APPEND);
Good luck.
祝你好运。
回答by Frxstrem
After reading your code and responses, I have come up with a theory...
阅读您的代码和回复后,我提出了一个理论...
Since I can't see that there's anything wrong with your code, how did you open and read the file? Did you actually open it in a text editor? Did you use a PHP script to do it?If so, open the file with a text editor and check that there are actually spaces at the end of each line. If there is actually is...well, ignore the rest of this answer, then. If not, just read on.
由于我看不出你的代码有什么问题,你是如何打开和读取文件的?你真的是用文本编辑器打开的吗?您是否使用 PHP 脚本来执行此操作?如果是这样,请使用文本编辑器打开文件并检查每行末尾是否确实有空格。如果确实存在......那么,请忽略此答案的其余部分。如果没有,请继续阅读。
For instance, if you use something like this:
例如,如果你使用这样的东西:
<?php
$lines = file($filename);
if($lines) // Error reading
die();
foreach($lines as $line)
echo $line."<br />";
Then you would always a whitespace at the end of the line because of the way file()
work. Make sure each$line
does not have a whitespace - such as a newline character- at the end.
Since HTML handles all whitespaces - spaces, tabs, newlinesetc. - as spaces, if there is a whitespace at the end of $line
, then those would appear as spaces in the HTML output.
然后,由于file()
工作方式,您总是会在行尾留一个空格。确保每个末尾$line
都没有空格 - 例如换行符- 。
由于 HTML 将所有空格 - 空格、制表符、换行符等 - 作为空格处理,如果在 末尾有空格$line
,那么这些将在 HTML 输出中显示为空格。
Solution: use rtrim($line)
to remove whitespaces at the end of the lines. Using the following code:
解决方案:用于rtrim($line)
删除行尾的空格。使用以下代码:
<?php
$lines = file($filename);
if($lines) // Error reading
die();
foreach($lines as $line)
echo rtrim($line)."<br />";
wouldn't have the same problems as the first example, and all spaces at the end of the lines would be gone.
不会有与第一个示例相同的问题,并且行尾的所有空格都将消失。
回答by matt
its because each time you write to the file, the file is being finished, file_put_contents
inserts an extra line break at the end
这是因为每次写入文件时,文件正在完成,file_put_contents
在末尾插入一个额外的换行符