New line in PHP output in a text file

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

    Check the filter that you're using to clean form input. It may be removing the 0x0a NewLine characters from your form input.

    I have text saved from a 'textarea' tag and it's correct. Using a HEX viewer, I see that each newline from the "textarea" field gets translated to hex character 0a. (decimal 10) Wikipedia lists this as the UTF-8 "newline character" or "LF." LF was short for Line Feed, from the old teletype days. https://en.wikipedia.org/wiki/UTF-8

    So, the lines test test test translate to "4 65 73 74 0d 0a 74 65 73 74 0d 0a 74 65 73 74" Note the two "0a" characters in the string, which are equivalent to \n.

    This wasn't apparent at first because the regular expression I was using to clean the input was replacing the 0a character with a space.

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

    As other answers state, you need to add in an end of line character after each field.

    Different OS's use different line endings, though, and so a "\n" may not display as a new line on Windows, for example. As Mahdi said, you can use Windows style "\r\n" line endings, or you can use the PHP_EOL constant so that line endings appropriate to the server will be output, in which case your code would look like

    $data = $_POST['name'] . PHP_EOL;
    $data .= $_POST['email'] . PHP_EOL;
    $data .= $_POST['message'] . PHP_EOL;
    
    0 讨论(0)
提交回复
热议问题