Preserve Line Breaks From TextArea

PhpMysqlHtml

Php Problem Overview


I'm using a textarea to enable users to input comments. However, if the users enters new lines, the new lines don't appear when they are outputted. Is there any way to make the line breaks stay.

Any idea how do preserve the line breaks?

Php Solutions


Solution 1 - Php

Two solutions for this:

  1. PHP function nl2br():

e.g.,

    echo nl2br("This\r\nis\n\ra\nstring\r");

    // will output
    This<br />
    is<br />
    a<br />
    string<br />

2. Wrap the input in <pre></pre> tags.

See: W3C Wiki - HTML/Elements/pre

Solution 2 - Php

Here is what I use

$textToOutput = nl2br(htmlentities($text, ENT_QUOTES, 'UTF-8'));

$text is the text that needs to be displayed $textToOutput is the returned text from nl2br and htmlentities so it can be safety displayed in the html context.
ENT_QUOTES will convert both double and single quotes, so you'll have no trouble with those.

Solution 3 - Php

Got my own answer: Using this function from the data from the textarea solves the problem:

function mynl2br($text) { 
   return strtr($text, array("\r\n" => '<br />', "\r" => '<br />', "\n" => '<br />')); 
} 

More here: http://php.net/nl2br

Solution 4 - Php

i am using this two method steps for preserve same text which is in textarea to store in mysql and at a getting time i can also simply displaying plain text.....

step 1:

$status=$_POST['status'];<br/>
$textToStore = nl2br(htmlentities($status, ENT_QUOTES, 'UTF-8'));

In query enter $textToStore....

step 2:

> write code for select query...and direct echo values....

It works

Solution 5 - Php

This works:

function getBreakText($t) {
    return strtr($t, array('\\r\\n' => '<br>', '\\r' => '<br>', '\\n' => '<br>'));
}

Solution 6 - Php

function breakit($t) {
    return nl2br(htmlentities($t, ENT_QUOTES, 'UTF-8'));
}

this may help you

pass the textarea wal

Solution 7 - Php

why make is sooooo hard people when it can be soooo easy :)

//here is the pull from the form
$your_form_text = $_POST['your_form_text'];


//line 1 fixes the line breaks - line 2 the slashes
$your_form_text = nl2br($your_form_text);
$your_form_text = stripslashes($your_form_text);

//email away
$message = "Comments: $your_form_text";
mail("[email protected]", "Website Form Submission", $message, $headers);

you will obviously need headers and likely have more fields, but this is your textarea take care of

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHirveshView Question on Stackoverflow
Solution 1 - PhpsuperUntitledView Answer on Stackoverflow
Solution 2 - PhphirokiView Answer on Stackoverflow
Solution 3 - PhpHirveshView Answer on Stackoverflow
Solution 4 - PhpJayendra BariyaView Answer on Stackoverflow
Solution 5 - PhpUbiQueView Answer on Stackoverflow
Solution 6 - PhpLibin ThomasView Answer on Stackoverflow
Solution 7 - PhpDuncanView Answer on Stackoverflow