New line in PHP output in a text file

前端 未结 8 2205
甜味超标
甜味超标 2021-01-04 17:05

My php form which saves the output in a text file glues the result to one string like:

Name1Email1The Message1Name2Email2The Message2Name3Email3The Message3Name4Emai

8条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-04 17:33

    When you concatenate your input values like that you won't have any newlines; you can add them using the literal "\n" or "\r\n". To string them all together you can use the concatenation operator or use sprintf() like below:

    $data = sprintf("%s\n%s\n%s\n", $_POST['name'], $_POST['email'], $_POST['messge']);
    $file = "YOURDATAFILE.txt"; 
    file_put_contents($file, $data, FILE_APPEND);
    

    I also use file_put_contents() with the FILE_APPEND flag to append data to a file and save a few more lines of code.

提交回复
热议问题