file_put_contents, file_append and line breaks

后端 未结 7 1265
旧时难觅i
旧时难觅i 2020-12-30 19:32

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 f

相关标签:
7条回答
  • 2020-12-30 19:41

    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.

    0 讨论(0)
  • 2020-12-30 19:45

    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

    0 讨论(0)
  • 2020-12-30 19:48

    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

    0 讨论(0)
  • 2020-12-30 20:00

    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

    0 讨论(0)
  • 2020-12-30 20:03

    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.

    0 讨论(0)
  • 2020-12-30 20:04

    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.

    0 讨论(0)
提交回复
热议问题