How to Replace Newline or \R\N With <Br/>

How can I replace newline or \r\n with br/?

There is already the nl2br() function that inserts <br> tags before new line characters:

Example (codepad):

<?php
// Won't work
$desc = 'Line one\nline two';
// Should work
$desc2 = "Line one\nline two";

echo nl2br($desc);
echo '<br/>';
echo nl2br($desc2);
?>

But if it is still not working make sure the text $desciption is double-quoted.

That's because single quotes do not 'expand' escape sequences such as \n comparing to double quoted strings. Quote from PHP documentation:

Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.

JavaScript replace \n with br /

You need the /g for global matching

replace(/\n/g, "<br />");

This works for me for \n - see this answer if you might have \r\n

NOTE: The dupe is the most complete answer for any combination of \r\n, \r or \n

var messagetoSend = document.getElementById('x').value.replace(/\n/g, "<br />");console.log(messagetoSend);
<textarea id="x" rows="9">    Line 1            Line 2                    Line 3</textarea>

How to REPLACE \n with br in PHP

If you are echo'ing the data out and you see an actual \n in the output, then what you have isn't not a newline character, but a literal \ followed by n character. Usually these are translated to a newline if in code, but if in string in a variable (such as when pulled from a database), it isn't. You should then do a str_replace on '\n' (notice the single quotes so it isn't translated to a newline character).

This is likely either done by you on insert using something like addslashes (would covert a \n newline to \\n) or possibly done automatically by the database for some arbitrary security reason and not translated back.

How do I replace all line breaks in a string with br / elements?

This will turn all returns into HTML

str = str.replace(/(?:\r\n|\r|\n)/g, '<br>');

In case you wonder what ?: means.
It is called a non-capturing group. It means that group of regex within the parentheses won't be saved in memory to be referenced later.
You can check out these threads for more information:

https://stackoverflow.com/a/11530881/5042169
https://stackoverflow.com/a/36524555/5042169

replace \n and \r\n with br / in java

It works for me.

public class Program
{
public static void main(String[] args) {
String str = "This is a string.\nThis is a long string.";
str = str.replaceAll("(\r\n|\n)", "<br />");
System.out.println(str);
}
}

Result:


This is a string.<br />This is a long string.

Your problem is somewhere else.

Replace \n with br /

Just for kicks, you could also do

mytext = "<br />".join(mytext.split("\n"))

to replace all newlines in a string with <br />.

How can i convert/replace every newline to 'br/'?

You need to use html_safe if you want to render embedded HTML:

<%= @the_string.html_safe %>

If it might be nil, raw(@the_string) won't throw an exception. I'm a bit ambivalent about raw; I almost never try to display a string that might be nil.

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>

Replace newline into br / and save it to database

For the reason I already specified, you don't want to put HTML inside the database unless you're sure it will only contain HTML.

For instance, let's say my name is "Chris Völkel", if you store it as HTML in the DB, it will look like: "Chris Völkel".
The problem with this is that you can't use the name anywhere else where it won't be HTML.

For example, your client might ask you to export the DB data into a CSV file. You will end up giving the client HTML inside the CSV file - not ideal, right?

Instead, you should encode the data to HTML when you render it on the web browser:

$data = $_POST['data'];
// ...
$stmt->bind_param($data);

And later on...

echo nl2br(htmlspecialchars($data));


Related Topics



Leave a reply



Submit