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?
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.
Good luck.
pauls answer has the correct approach but he has a mistake. what you need ist the following:
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
its because each time you write to the file, the file is being finished,
file_put_contents
inserts an extra line break at the endThere 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 usetrim($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 number0
instead, useintval($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, useintval()
, 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.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:
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:wouldn't have the same problems as the first example, and all spaces at the end of the lines would be gone.
Did you tried with PHP EOL constant?
--- 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
You could try to parse the file contents using php to see what's inside
...tricky