Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture newline from a textarea input

Tags:

I have a textarea form in my html. If the user hits enter between 2 sentences that data should be carried over to my PHP.

Currently if the user enters:

Apple
Google
MS

and my PHP code is:

$str = $_POST["field"];

echo $str;

I get

Apple Google MS 

as the output. I want output to be like this

Apple
Google
MS

what should I do?

like image 608
Bilbo Baggins Avatar asked Apr 30 '11 20:04

Bilbo Baggins


People also ask

How do you make a new line in textarea?

To add line breaks to a textarea, use the addition (+) operator and add the \r\n string at the place where you want to add a line break, e.g. 'line one' + '\r\n' + 'line two' . The combination of the \r and \n characters is used as a newline character.

How do you preserve a new line in HTML?

The <pre> tag defines preformatted text. Text in a <pre> element is displayed in a fixed-width font, and the text preserves both spaces and line breaks. The text will be displayed exactly as written in the HTML source code.

Which character defines a new line in the textarea?

Talking specifically about textareas in web forms, for all textareas, on all platforms, \r\n will work.


1 Answers

Try nl2br() instead:

echo nl2br($str);
like image 86
Alix Axel Avatar answered Sep 19 '22 13:09

Alix Axel