Forum Moderators: coopster

Message Too Old, No Replies

Simple simple string question

         

wavesurf

7:11 pm on Oct 12, 2005 (gmt 0)

10+ Year Member



Hi -

I have a really simple question, bordering on stupid :)

When I take the input from a textarea and insert it into a mysql-base and then retrieve it and show it, all the line feeds/breaks are gone. It is shown as one giant string. How can this be fixed, so the text will be shown as it was entered? Does it have something to do with stripslashes/addslashes or am I way off?

Sorry for the dumb question, but for the life of me I can't remember or find out how this is done.

Thanks for any help...

mincklerstraat

7:23 pm on Oct 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First off (I'll assume you know this, just mentioning in case you don't) -- if your text is re-appearing somewhere outside the textarea, it's <br /> you need, while the linebreaks "come in" via the text area as \n (or \r\n maybe for windows), so you can use nl2br() to change the "ordinary" linebreaks into HTML linebreaks.

If it's not this, you could be having problems with not escaping the input before it goes into the query. All input that goes into mysql queries should be escaped with mysql_real_escape_string().

Hope this helps.

henry0

7:43 pm on Oct 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Strange I was just looking at the manual

here is a little addon:


A note to add to the br2nl. Since nl2br doesn't remove the line breaks when adding in the <br /> tags, it is necessary to strip those off before you convert all of the tags, otherwise you will get double spacing. Here is the modified function:

function br2nl($str) {
$str = preg_replace("/(\r\nŠ\nŠ\r)/", "", $str);
return preg_replace("=<br */?>=i", "\n", $str);
}