Reputation: 187
I can't get nl2br
function to work after fetching data from my database:
$result = mysql_query("SELECT comments..etc.etc..");
while ($row = mysql_fetch_array($result))
{
echo nl2br($row["comments"]);
}
In database row comments
:
\r\nThanks,\r\n
OUTPUT:
Same as in DB:
\r\nThanks,\r\n
If I simply test this out like so it works fine:
<?php
$mystring = "\r\nThanks,\r\n";
echo nl2br($mystring);
?>
OUTPUT:
converts \r \n to <br />
Upvotes: 15
Views: 30733
Reputation: 1183
This could be a solution, at least it was for me.
$message = str_replace("\\r\\n", "<br>", $message);
It is possible that you are getting a string in which the slashes are escaped (i.e. \\) and nl2br
works with \n\r not \\n\\r
Once you understand this, the solution is easy :)
Upvotes: 0
Reputation: 1036
I was able to replace newline with <br>
using this code :
str_replace(array("\r\n", "\r", "\n"), "<br>", "input");
(windows machine)
Upvotes: 0
Reputation: 149
Not working for me either. I just did the following:
$mensaje = str_replace("
", "<br/>
", $mensaje);
Upvotes: 0
Reputation: 111
For some reason this didn't work for me...
echo nl2br(stripcslashes($row["comments"]));
But this did...
$comments = stripcslashes($row["comments"]);
$commentsWithBreaks = nl2br($comments);
echo $commentsWithBreaks;
Upvotes: 2
Reputation: 17555
try this:
echo preg_replace('/\v+|\\\r\\\n/Ui','<br/>',$row["comments"]);
Upvotes: 37
Reputation: 21
Make sure that you are not to using strings from file and/or set in single apostrophe. Because string is treated literally, and nl2br will not work. NL2BR will work with double apostrophe.
Upvotes: 2
Reputation: 1072
Data you have stored is allready added the slashes.
You have to use stripslashes() first then str_replace()
stripslashes(str_replace('\r\n','<br/>',$row["comments"]))
Upvotes: 1
Reputation: 846
I know this is an old post but if like me you are have stumbled across this issue and the above didn't work for you, this solution may help you instead:
echo nl2br(stripslashes($row["comments"]));
or (they are not the same function, note the additional "c" after strip)
echo nl2br(stripcslashes($row["comments"]));
See original thread that helped me: nl2br() not working when displaying SQL results
Upvotes: 18
Reputation: 1317
Following solution will work for both windows as well as for linux/unix machine
str_replace(array("\\r\\n", "\\r", "\\n"), "<br />", "string");
Upvotes: 5
Reputation: 157839
Most likely you are doing escaping twice, when adding your data into DB.
Check your code that adds data to DB and remove unnecessary escaping.
Most likely it's some senseless "universal sanitization" function.
Well it's easy.
Let's take a quote, not a newline to demonstrate. The behavior the same.
Slashes being stripped then data goes to database.
Thus, in the normal case:
source: It's
after escaping: It\'s
by the query execution slash being stripped and
both in the database and back It's
in double escaping case:
source: It's
after escaping: It\'s
after second escaping: It\\\'s
by the query execution slash being stripped and
both in the database and back It\'s
we have our data spoiled.
Just make yourself understand that escaping i not something magical that makes your data "safe" (and, therefore can be done many times, as you probably think). It's just adding a backslash to certain symbols.
Upvotes: 8
Reputation: 9705
Building on what Christian is saying, why don't you trying replacing the literal '\r\n'
with "\r\n"
?
Upvotes: 1
Reputation: 8125
My guess is that the slashes in your DB are literal slashes (followed by n or r), not newlines. Can you find a way to store literal newlines in your database?
Upvotes: 4