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

PhpHtmlLine BreaksNl2br

Php Problem Overview


I am trying to simply replace some new lines and have tried three different ways, but I don't get any change:

$description = preg_replace('/\r?\n|\r/', '<br/>', $description);
$description = str_replace(array("\r\n", "\r", "\n"), "<br/>", $description);
$description = nl2br($description);

These should all work, but I still get the newlines. They are double: "\r\r". That shouldn't make any of these fail, right?

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

Try using this:

$description = preg_replace("/\r\n|\r|\n/", '<br/>', $description);

Solution 3 - Php

You may have real characters "" in the string (the single quote strings, as said @Robik).

If you are quite sure the '\r' or '\n' strings should be replaced as well, I'm not talking of special characters here but a sequence of two chars '' and 'r', then escape the '' in the replace string and it will work:

str_replace(array("\r\n","\r","\n","\\r","\\n","\\r\\n"),"<br/>",$description);

Solution 4 - Php

Try this:

echo str_replace(array('\r\n', '\n\r', '\n', '\r'), '<br>', $description);

Solution 5 - Php

nl2br() as you have it should work fine:

$description = nl2br($description);

It's more likely that the unclosed ' on the first line of your example code is causing your issue. Remove the ' after $description...

...$description');

Solution 6 - Php

nl2br() worked for me, but I needed to wrap the variable with double quotes:

This works:

$description = nl2br("$description");

This doesn't work:

$description = nl2br($description);

Solution 7 - Php

This will work for sure:

str_replace("\\r", "<br />", $description); 
str_replace("\\n", "<br />", $description); 

Solution 8 - Php

$description = nl2br(stripcslashes($description));

Solution 9 - Php

I think str_replace(array("\\r\\n", "\\r", "\\n"), " ", $string); will work.

Solution 10 - Php

If you are using nl2br, all occurrences of \n and \r will be replaced by <br>. But if (I don’t know how it is) you still get new lines you can use

str_replace("\r","",$description);
str_replace("\n","",$description);

to replace unnecessary new lines by an empty string.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontheflowersoftimeView Question on Stackoverflow
Solution 1 - PhpRobikView Answer on Stackoverflow
Solution 2 - PhpafarazitView Answer on Stackoverflow
Solution 3 - PhpregileroView Answer on Stackoverflow
Solution 4 - Phpl2aelbaView Answer on Stackoverflow
Solution 5 - PhpsakatcView Answer on Stackoverflow
Solution 6 - PhpinfografnetView Answer on Stackoverflow
Solution 7 - PhpRadeckView Answer on Stackoverflow
Solution 8 - Phpsoftcod.comView Answer on Stackoverflow
Solution 9 - PhpThunderStormView Answer on Stackoverflow
Solution 10 - PhpEvgeniy SkulditskyView Answer on Stackoverflow