I am writing a blog type script and want to keep the return (enter key) so new paragraphs will start on a different line and lines can be separated. Currently if someone enters:
Line 1
Line 2
It will store and retrieve without separation as:
Line 1 Line 2
I thought addslashes() was what I needed but that is not working. The data is stored in the MySQL data field as text and I have magic_quotes_gpc turned on if that matters.
Turn Magic Quotes off now. Turn Register Globals off now.
It is very likely at this point that the PHP team will follow through with the deprecation of these features and REMOVE them starting with version 5.4. (6.0 has been pushed off indefinitely).
So if you still write code requiring them that code will FAIL when 5.4 comes out.
Using the function is the same principle as using nl2br().
function nl2p($pee, $skip=null) {
if($skip) {
return $pee;
} else {
// remove any carriage returns (mysql)
$string = str_replace("\\r", '', $pee);
// replace any newlines that aren't preceded by a > with a <br />
$string = preg_replace('/(?<!>)\
/', "<br />\
", $string);
return $string;
}
}
// to make it add the line breaks
$row['message'] = nl2p($row['message']);
The only reason I had the $pee skip part was because I had it as part of a CMS that served different data types that might not have needed line breaks