Writing TXT File with PHP, Want to Add an Actual Line Break

后端 未结 5 1512
天命终不由人
天命终不由人 2020-12-16 13:29

I am writing a TXT file using PHP. I want to insert actual line breaks into the TXT file wherever necessary. I have tried all combinations of \\n \\r \\r\\n \\n\\r ... but t

相关标签:
5条回答
  • 2020-12-16 13:50

    Sounds to me like you might be using single quotes, i.e. '\n' rather than "\n".

    If you wanted to continue with a single quotes bias (as you should!), two options:

    file_put_contents('/path/to/file.txt', 'Hello friend!
    This will appear on a new line.
    As will this');
    
    // or
    
    file_put_contents('/path/to/file.txt', 'Hello friend!'."\n".'This will appear on a new line.'."\n".'As will this');
    
    0 讨论(0)
  • 2020-12-16 13:53

    \r\n in a windows server \n in linux Make sure you upload the file as ASCII.

    0 讨论(0)
  • 2020-12-16 13:57

    you could also use chr(10) which is line break.

    0 讨论(0)
  • 2020-12-16 13:59

    For "\n" to work, you need to use double quotes, not '\n'.

    But you should use the constant PHP_EOL instead, so that it adapts automatically to the OS ("\n", "\r" or "\r\n").

    file_put_contents('file.txt', 'Bla' . PHP_EOL . 'Bla');
    
    0 讨论(0)
  • 2020-12-16 14:04

    You must write \n in a double-quoted string (in single-quoted strings no parsing takes place):

    "foo\r\nbar"
    

    Further reference:

    http://es2.php.net/manual/en/language.types.string.php

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