开发者

nl2br not working for me

I ca开发者_Python百科n'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 />


try this:

echo preg_replace('/\v+|\\\r\\\n/Ui','<br/>',$row["comments"]);


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


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.


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?


Following solution will work for both windows as well as for linux/unix machine

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


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.


Building on what Christian is saying, why don't you trying replacing the literal '\r\n' with "\r\n"?


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"]))


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;


Not working for me either. I just did the following:

$mensaje = str_replace("&#10;", "<br/>", $mensaje);


I was able to replace newline with <br> using this code :

str_replace(array("\r\n", "\r", "\n"), "<br>", "input");

(windows machine)


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 :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜