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.
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.
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.
Talking specifically about textareas in web forms, for all textareas, on all platforms, \r\n will work.
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>
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>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With