tags:

views:

107

answers:

4

Hi all,

I can't get nl2br function to work after fetching data from my DB...

$result = mysql_query("SELECT comments..etc.etc..");

while ($row = mysql_fetch_array($result))
{
  echo nl2br($row["comments"]);
}

In DB 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 />
+3  A: 

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?

Christian Mann
+1  A: 

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

blockhead
Because it must be decease do be cured, not symptom.
Col. Shrapnel
+1  A: 

try this:

echo preg_replace('/\v+|\\\[rn]/','<br/>',$row["comments"]);
stillstanding
nice approach. it replaces both the real newlines and literal newlines.
stillstanding
excellent approach. and then another one for output in plain text. I can't believe there are too many direct-minded people around, who can't move a step aside from what they read. "Answer question as it phrased and never think of it's background" - a real SO motto. No matter if an answer is actually a disservice.
Col. Shrapnel
I would suggest replacing \v+|\\\r\\\n first to get those line-endings (Mac/most Unix) and replacing any remaining \\\n for the remaining line endings. The regular expression you have listed will insert double the amount of line breaks for Mac/most Unix. Otherwise, this answers sebb's question.
greg
+1  A: 

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.

Col. Shrapnel
You're not answering the question. Maybe he has no control over how the data get's into his database? Either way, it's in there now. Fixing code which is senselessly sanitizing his data will not help his problem.
blockhead
that's simple, dude. Not every question deserves an answer.
Col. Shrapnel
@Col. Sharpnel HI, thanks for the tip, youre right in my code I was doing escaping twice inadvertently , Im trying to understand , what is the process that happens when you escape twice? can you show me so i understand what happens to data when you escape twice? thanks
sebb
@sebb isn't it obvious? updated my answer.
Col. Shrapnel