Click to See Complete Forum and Search --> : Line break problem


tanfwc
10-07-2006, 12:07 PM
Hi,

After trying to search for solution, I can't seem to resolve this. Hope someone can guide me while I learn PHP

$comment variable will be fill in by sql query. The result return is

Test Comment 3\r\nTest Comment 3\r\nTest Comment 3\r\nTest Comment 3
str_replace("\r\n", "<br />", $comment)

Thank you in advance

NogDog
10-07-2006, 02:41 PM
And the problem/question is?....

tanfwc
10-07-2006, 02:46 PM
Oh, sorry. How to replace \r\n with <br /> after getting the record from sql?

NogDog
10-07-2006, 02:50 PM
You could just use the nl2br() function (http://php.net/nl2br).

Neoboffin
10-07-2006, 02:54 PM
This would make $comment become the new $comment.
$comment = str_replace("\r\n", "<br />", $comment);

tanfwc
10-07-2006, 03:06 PM
Thanks for the headup...but strange, it still return the same result...

tanfwc
10-07-2006, 03:09 PM
This would make $comment become the new $comment.
I did try to print the new variable, it always return the same result query from the DB

Neoboffin
10-07-2006, 03:17 PM
Can you show some of your script because what you trying to do should work.

tanfwc
10-07-2006, 03:32 PM
while( $row = mysql_fetch_array( $result )) {
$comment = $row["tablename"];
$test = str_replace("\r\n", "<br />", $comment);
echo $test;
}

Is this enough?

Neoboffin
10-07-2006, 03:41 PM
That should work. If not, try just having this:

$test = str_replace("\n", "<br />", $comment);

tanfwc
10-07-2006, 03:46 PM
this is killing me...it still return the same result...tks for the help anyway

NogDog
10-07-2006, 04:00 PM
Are you sure it's "\r\n" and not just "\n" in the DB? Have you tried using the nl2br() function which already exists in PHP? Have you looked at the "view source" screen of your browser to see exactly what has been output?

If you do not want to use nl2br(), I'd recommend this:

$test = preg_replace('/\r?\n/', "<br />\n", $comment);

tanfwc
10-07-2006, 04:03 PM
Tks lot NogDog..but i try, still fail...

anyway, I manage to fix the problem.

$test = str_replace("\\r\\n", "<br />", $comment);

NogDog, I am very sure in the db, it is \r\n

I'm still wondering why I need to do double \?

NogDog
10-07-2006, 04:22 PM
That suggests to me that what is in the DB is literally a sequence of backslash, "r", backslash, "n" instead of a carriage-return character followed by a newline character.