Solution information:
The nl2br function does not replace \\n
with <br />
as I had expected but inserts a <br />
before the \\n
.
From php.net:
nl2br — Inserts HTML line breaks before all newlines in a string
Original question:
I'm replacing <br />
elements with \\n
in PHP, this is my input:
Top spot!<br />
<br />
123456789 123456789 123456789
This is my code:
$commenttext = str_replace("<br />", "\n", $reviewdata['comment']);
But my output is:
Top spot!
123456789 123456789 123456789
Is there something I'm missing with using str_replace? I'm getting double the breaks returned after using it.
Let me show you. Your code before replacement:
Top spot!<br />\n<br />\n123456789 123456789 123456789
Your code after replacement:
Top spot!\n\n\n\n123456789 123456789 123456789
As you can see <br />
was correctly replaced with new line.
Try to replace <br />
tags with the new lines first:
$commenttext = str_replace("<br />\n", "\n", $reviewdata['comment']);
<?php
$text = 'top spot!<br />
<br />
123456789 123456789 123456789';
echo str_replace("<br />\n","\n",$text);
?>
OUTPUT
top spot! 123456789 123456789 123456789
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.