Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting <br /> into a new line for use in a text area

Tags:

html

php

If I have a variable:

$var1 = "Line 1 info blah blah <br /> Line 2 info blah blah"; 

And a text area:

<textarea>echo $var1</textarea> 

How can I get the text area to display a new line instead of displaying the text on a single like with a <br /> in it?

Edit: I have tried the following:

<textarea class="hobbieTalk" id="hobbieTalk" name="hobbieTalk" cols="35" rows="5" onchange="contentHandler('userInterests',this.id,this.value,0)"><?php  $convert=$_SESSION["hobbieTalk"]; $convert = str_replace("<br />", "\n", $convert); echo $convert;  ?></textarea> 

However the text area still contains the br tags in the lines.

like image 589
someguy Avatar asked May 14 '11 19:05

someguy


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 add a new line character in HTML?

To add a line break to your HTML code, you use the <br> tag. The <br> tag does not have an end tag. You can also add additional lines between paragraphs by using the <br> tags. Each <br> tag you enter creates another blank line.

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.


2 Answers

Try this one

<?php     $text = "Hello <br /> Hello again <br> Hello again again <br/> Goodbye <BR>";     $breaks = array("<br />","<br>","<br/>");       $text = str_ireplace($breaks, "\r\n", $text);   ?>   <textarea><?php echo $text; ?></textarea> 
like image 164
Mobilpadde Avatar answered Sep 20 '22 02:09

Mobilpadde


i am use following construction to convert back nl2br

function br2nl( $input ) {     return preg_replace('/<br\s?\/?>/ius', "\n", str_replace("\n","",str_replace("\r","", htmlspecialchars_decode($input)))); } 

here i replaced \n and \r symbols from $input because nl2br dosen't remove them and this causes wrong output with \n\n or \r<br>.

like image 23
aftamat4ik Avatar answered Sep 22 '22 02:09

aftamat4ik