file_put_contents, file_append and line breaks

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

提交回复
热议问题