Converting <Br /> into a New Line for Use in a Text Area

New Line in Textarea to be converted to br/

This will replace line breaks to HTML break tags. The different combinations are to cover the different browsers/systems and how line breaks are interpreted.


$(this).val().replace(/\r\n|\r|\n/g,"<br />")

This will bring it back to new lines - also covering how different browsers interpret innerHTML.


boxText.replace(/<br\s?\/?>/g,"\n");

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

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>

converting BR tags into new lines in a textarea

Probably your HTML is

foo<br>
bar<br>
nuts<br>

So you already have "\n" and replacing br to "\n" you ends up with double "\n" like

foo \n\n bar \n\n nuts

it's looks like

foo

bar

nuts

For receiving output you suggest - you need to remove "\n" from the input HTML and then replace br to "\n"

The code would be

function br2nl($st){
$st_no_lb = preg_replace( "/\r|\n/", "", $st );
return preg_replace('/<br(\s+)?\/?>/i', "\n", $st_no_lb);
}

changing new line to br in text area

have you tried the php constant PHP_EOL? in you str_replace code?

$open=str_replace(PHP_EOL,"<br>",$_POST["open"]);

How to add a new line in textarea element?

Try this one:

    <textarea cols='60' rows='8'>This is my statement one.
This is my statement2</textarea>

newline character not converting to line break in php text area post

You need to use an actual linebreak in you <textarea>. \n wont work there.

nl2br converts linebreaks to html <br>.

With php-code you can add a linebreak like this:

<textarea name="comment" rows="5" cols="40">One line.<?= "\n" ?>Another line.</textarea>

Or you spimply write:

<textarea name="comment" rows="5" cols="40">One line.
Another line.</textarea>

If you want to reuse the submitted value:

<textarea name="comment" rows="5" cols="40"><?= htmlentities($_POST['comment']) ?></textarea>

Be careful here, you want to sanitize your user-input with htmlentities($_POST['comment']) or the user will be able to alter the page.

Additional info

<?= $var ?> is the short form of <?php echo $var; ?>

References

  • htmlentities

Converting \r\n to line breaks in a textarea (not using a br tag) in PHP

PHP has two different string building modes. The first uses single quotes, and will do absolutely no variable or special character substitutions. That's what you're using.

The second is variable-embedding in double quoted strings, which should work:

$text = str_replace("\\", "\", $text);
echo "<textarea>$text</textarea>";

The \r and \n should now be active carriage return and newline characters in your output, not the character "slash" and then an 'r' or 'n'

Javascript how to convert \r \n line breaks in textarea?

You can try this For New line.

function output(v)

{

var str = v.replace(/(?:\\n)/g, '\n');

$("#txt2").val(str);

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

input:<br /><textarea id="txt1" style="resize:none; width: 400px;height: 180px;" onKeyUp="output(this.value)"></textarea>

<br />

output:<br /><textarea id="txt2" style="resize:none; width: 400px;height: 180px;" ></textarea>

replace '\r\n' with ' br ' in Text area javascript?

Your code will work fine if:-

1.bind() converted to on()(because bind() is deprecated)

2.text() need to be .val()

Working example (check comments too):-

// applied mouseout to prevent repetition on each key-press

//you can apply keyup also no problem

$('#inputText').on('mouseout', function(e) {

var data = $('#inputText').val();

$('#inputText').val(data.replace(/\n/g, "<br />")); // text() need be val()

}); // here ); missing in your given code
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<textarea id="inputText"></textarea>


Related Topics



Leave a reply



Submit