file_put_contents, file_append and line breaks

后端 未结 7 1313
旧时难觅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: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

提交回复
热议问题