Forum Moderators: coopster

Message Too Old, No Replies

nl2br is broken

         

too much information

4:25 am on Oct 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I feel like I'm working with a completely messed up language, most of the typical assumptions you would have with PHP are not working and it's only on the new pages I'm working on, the old stuff is working fine.

Is there any reason that nl2br would not replace newlines with <br>?

I'm using this in an SQL statement to insert a block of text into my MySQL database. Every time I try this the <br>'s are never there and the line breaks are.

I even used this function from another thread in this forum and it doesn't work:

$content = explode("\n",$message);
foreach($content as $key => $line) {
$lines[$key] = $line;
}
$content = implode("<br>",$lines);

Even if I try to replace \r or the combination of \r\n it still doesn't work.

I need some guidance...

Let me add a little more to this...

I'm collecting the text in a <textarea> of a form, then getting the data using:

if(get_magic_quotes_gpc()) {
$message = mysql_real_escape_string(stripslashes($_POST["message"]));
} else {
$message = mysql_real_escape_string($_POST["message"]);
};

Then adding:
$message = nl2br($message);

and my SQL statement:
"insert into table (Message) values ('".$message."')"

but the text appears in the database just as I typed it, no newlines replaced.

cameraman

7:39 am on Oct 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mysql_real_escape_string prepends backslashes to newlines - try the nl2br beforehand.

too much information

12:12 am on Oct 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That did the trick!

Thanks for the help, it's the first time I have used the mysql_real_escape_string... I thought I was going crazy! ;o)