line breaks in a textarea
Asked Answered
A

13

44

I know when saving a textarea you can use the nl2br() or str_replace to change the /n to br tags etc. However what im not sure about how to insert line breaks into a textarea. I cant seem to find much about putting the data back into a textarea with those line breaks.

For example I have a form where users can update fields. So the user may enter:

foo
bar
baz

When that is saved to the database it would be saved as:

foo<br />bar<br />baz<br />

Now when that user goes back to that form after a page refresh all the fields are automatically populated with their previous data by taking the data from the database.

However the textarea shows the br tags as text instead of adding in the line breaks. i also tried changing the br tags to /n hoping the textarea would interpret these as line breaks but no joy. As well as this I also tried escaping etc.

So my questions are can this be done? Or more importantly can it be done using HTML/PHP (im using smarty). If that isnt possible can it be done using javascript?

Examples would be appreciated.

thanks for reading

Addax answered 25/6, 2011 at 21:30 Comment(0)
B
74

Don't do nl2br when you save it to the database. Do nl2br when you're displaying the text in HTML. I can strongly recommend to not store any HTML formatting in the database (unless you're using a rich HTML editor as well, in which case it would be silly not to).

A newline \n will just become a newline in the textarea.

Brindisi answered 25/6, 2011 at 21:33 Comment(4)
thanks to everyone for their replies. I can now see that its better practice not to add html on the way in to the db but rather apply it if and when needed on the way out :) thank youAddax
@Addax well don't forget you must treat on the way in for security concern :)Napoleon
@Oddantfr Agreed :) I've just got into the habit of having an include file which covers all page related pre-procesing. So all POST and GET values are cleaned before any page related php is executed.Addax
One thing to point out is to use \n between " (double quotes) instead of ' (single quotes). Otherwise it won't work. Like this: "\n"Halinahalite
D
33

You could use str_replace to replace the <br /> tags into end of line characters.

str_replace('<br />', PHP_EOL, $textarea);

Alternatively, you could save the data in the database without calling nl2br first. That way the line breaks would remain. When you display as HTML, call nl2br. An additional benefit of this approach is that it would require less storage space in your database as a line break is 1 character as opposed to "<br />" which is 6.

Drillstock answered 25/6, 2011 at 21:33 Comment(1)
And of course if you're doing the replace on display instead of saving it to the database (which is definitely better, I think) then the code would be str_replace(PHP_EOL, '<br />', $textarea); ... or just nl2br($textarea) ...Necaise
S
25

Ahh, it is really simple

just add

white-space:pre-wrap;

to your displaying element css

I mean if you are showing result using <p> then your css should be

p{
   white-space:pre-wrap;
}
Salientian answered 5/9, 2013 at 9:1 Comment(2)
This will work only when you are inserting data in Textarea and displaying it in other element(at Front End)Salientian
if you want it to be exactly like <pre> (no wrapping other than on newline), then you can use p { white-space: pre; }.Cartilage
B
7

You can use following code:

$course_description = nl2br($_POST["course_description"]);
$course_description = trim($course_description);
Babylonia answered 19/5, 2015 at 22:2 Comment(0)
S
7

Some wrong answers are posted here. instead of replacing \n to <br />, they are replacing <br /> to \n

So here is a good answer to store <br /> in your mysql when you entered in textarea:

str_replace("\n", '<br />',  $textarea);
Spring answered 28/2, 2017 at 8:22 Comment(0)
C
3

My recommendation is to save the data to database with Line breaks instead parsing it with nl2br. You should use nl2br in output not input.

For your question, you can use php or javascript:

PHP:

str_replace('<br />', "\n", $textarea);

jQuery:

$('#myTextArea').val($('#myTextArea').val().replace(@<br />@, "\N"));
Cosmetician answered 25/6, 2011 at 21:36 Comment(0)
P
1

PHP Side: from Textarea string to PHP string

$newList = ereg_replace( "\n",'|', $_POST['theTextareaContents']);

PHP Side: PHP string back to TextArea string:

$list = str_replace('|', '&#13;&#10;', $r['db_field_name']);
Pipestone answered 6/2, 2014 at 20:8 Comment(0)
W
1

This works on me.

 str_replace(array("\r", "\n"), '&#10;', $textareavalue);
Worth answered 30/3, 2016 at 2:9 Comment(0)
D
1

The simple way:

  1. Use this to insert into mysql:

    $msg = $_GET['msgtextarea']; //or POST and my msg field format: text
    $msg = htmlspecialchars($msg, ENT_QUOTES);
  2. And use this for output:

    echo nl2br($br['msg']);
Dennett answered 23/3, 2017 at 2:28 Comment(0)
W
1

What I found works in the form is str_replace('&lt;br&gt;', PHP_EOL, $textarea);

Wellington answered 18/6, 2017 at 12:28 Comment(1)
this worked for my purposes and is super simple and flexbileTheresatherese
S
0

From PHP using single quotes for the line break worked for me to support the line breaks when I pass that var to an HTML text area value attribute

PHP

foreach ($videoUrls as $key => $value) {
  $textAreaValue .= $value->video_url . '\n';
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

HTML/JS

$( document ).ready(function() {
    var text = "<?= htmlspecialchars($textAreaValue); ?>";
    document.getElementById("video_urls_textarea").value = text;
});
Sinatra answered 11/7, 2016 at 20:56 Comment(0)
N
-1

I'm not sure this is possible but you should try <pre><textarea> ... </textarea></pre>

Napoleon answered 25/6, 2011 at 21:34 Comment(1)
@Nideo it's a bit tricky and not really neat if this is what you meant . But it works if you don't use any <br> but let what the user wroteNapoleon
A
-4
<?php
$smarty = new Smarty;
$smarty->assign('test', "This is a \n Test");
$smarty->display('index.tpl');
?>

In index.tpl

{$test|nl2br}

In HTML

This is a<br />
test
Addition answered 28/1, 2014 at 9:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.