file_put_contents, file_append and line breaks

2020-02-26 13:57发布

I'm writing a PHP script that adds numbers into a text file. I want to have one number on every line, like this:

1
5
8
12

If I use file_put_contents($filename, $commentnumber, FILE_APPEND), the result looks like:

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):

1_
5_
8_
12_
_
_

How do I get that function to add the numbers the way I want, without spaces?

7条回答
乱世女痞
2楼-- · 2020-02-26 14:33

annoyingregistration, what you have there is absolutely fine. PHP_EOL and "\n" are exactly the same.

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.

file_put_contents($filename, trim($commentnumber . "\n"), FILE_APPEND);

Good luck.

查看更多
ら.Afraid
3楼-- · 2020-02-26 14:35

pauls answer has the correct approach but he has a mistake. what you need ist the following:

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

查看更多
爷的心禁止访问
4楼-- · 2020-02-26 14:38

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

查看更多
小情绪 Triste *
5楼-- · 2020-02-26 14:40

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.

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).

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.

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.

查看更多
Luminary・发光体
6楼-- · 2020-02-26 14:43

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.

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, newlines etc. - as spaces, if there is a whitespace at the end of $line, then those would appear as spaces in the HTML output.

Solution: use rtrim($line) to remove whitespaces at the end of the lines. Using the following code:

<?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.

查看更多
淡お忘
7楼-- · 2020-02-26 14:50

Did you tried with PHP EOL constant?


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


$lines = explode( PHP_EOL, file_get_contents($file));
foreach($lines as $line ) {
    var_dump($line);
}

...tricky

查看更多
登录 后发表回答