You need to look at nl2br () along with trim () .
nl2br() will insert <br /> before the newline character ( \n ), and trim() will remove any trailing \n characters or whitespace characters.
$text = trim($_POST['textareaname']); // remove the last \n or whitespace character $text = nl2br($text); // insert <br /> before \n
This should do what you want.
UPDATE
The reason the following code will not work is because in order for \n be recognized, it must be inside double quotes, since they use double quote data, where single quotes take it literally, IE "\n"
$text = str_replace('\n', '<br />', $text);
To fix this, it would be:
$text = str_replace("\n", '<br />', $text);
But it is better to use the built-in nl2br() function provided by PHP.
EDIT
Sorry, I decided that the first question was that you could add line strings, indeed, this will change the answer a bit, since anytype explode() will remove line breaks, but here it is:
$text = trim($_POST['textareaname']); $textAr = explode("\n", $text); $textAr = array_filter($textAr, 'trim'); // remove any extra \r characters left behind foreach ($textAr as $line) { // processing here. }
If you do this like this, you need to add <br /> to the end of the line before processing is done on its own, since the explode() function will remove the \n characters.
Added array_filter() to trim() with any extra \r characters that may have been delayed.