New line in PHP output in a text file

前端 未结 8 2062
甜味超标
甜味超标 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:32

    you'r $data variable needs to already have the \n new line characters in it.

    0 讨论(0)
  • 2021-01-04 17:32

    this works for me:

    echo "$data"."\r\n"; 
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-04 17:36

    When we attempt to extract data submitted online via a Web-Form and to write the same to a text file which is placed on the web-server -- without overwriting the information already contained in the said text file, then we use the following code within the tags:

    <html>
    <body>
    <form action="" method="">
    <input value="submit">
    <div>Name: <input value=""></div>
    <div>email: <input value=""></div>
    ...           ...         ...
    <div><button>Submit</button></div>
    </form>
    </body>
    </html>
    

    The above HTML Code is coupled with the following PHP Code:

    <?php
    $fileName = 'fdata.txt';
    $fp = fopen('fdata.txt', 'a');
     $savestring = $_POST['name'] . ',' . $_POST['email'] ;
    fwrite($fp, $savestring);              
    fclose($fp);
    ?>
    

    But the above two Codes together lead to data that is appended and stringed together in the text file -- that is to say, every new record (set of data) extracted from subsequent Web-Forms does not begin at a new line (there is no line-break or carriage-return).

    I searched the Net for a Solution to the above Issue -- as suggested on the Net, I added “\r\n” (in double quotes) and also used ‘PHP_EOL’ (without quotes) as under:

    $fp = fopen('fdata.txt', 'a', "\r\n");
    $savestring =  “\r\n” . PHP_EOL .  ' ' . $_POST['name'] . ',' . $_POST['email'] . 
    PHP_EOL;
    

    Yet there was no line-break at all (my web-server and my desktop use Windows). It means that there was no practicable Solution within the knowledge of anyone of the millions of Users of Net in the World. It appears that the parameter ‘a’ overrides “\r\n”, ‘PHP_EOL’ etc.

    0 讨论(0)
  • 2021-01-04 17:41

    I usually combine New Line with Carriage Return:

    $data = $_POST['name'] . "\r\n";
    
    0 讨论(0)
  • 2021-01-04 17:47

    You can use "\n" to write a new line.

    $data = $_POST['name'] . "\n";
    $data .= $_POST['email'] . "\n";
    $data .= $_POST['message'] . "\n";
    

    Just as a sidenote, \n has to be in doublequotes. When surrounded by single quotes it won't work.

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