Simply I have a form and when users submit a post on my guest book I need when they press enter (line break) in the form that I have to do some kind of preg_replace and replace that linebreak with a break tag.
the variable holding the message is $msg
Thanks in advance for your help!
[man]nl2br/man
That's what you need....
If you need it in XHTML format (as your topic title suggests), do nl2br, and then do a simple str_replace():
$msg = nl2br($msg); $msg = str_replace('<br>', '<br />', $msg);
nl2br() adds linebreaks in the form of <br />, so the str_replace() is unnecessary.
Ah, didn't see that side-note about 4.0.5.... but then again, I haven't looked at that function's manual page since 3.x.... it's just been one of those I know, ya know?
Okay, so just [man]nl2br[/man] is what you're lookin for....
Sorry one more thing. You said that this is from the form.. So I must add this to the form part before the query to insert XHTML line breaks before I instert it to the database. Am I right?
How about after it is in the database whwn displaying stuff that is coming out??? I ask because I already have entries in the database. Is there any ways to change this or should I not even worry about it?
how about instead of <br /> I wanted something that added "</p><p>" This I ask because the way it is set up now the first line of text is indented.
[man]str_replace/man
All "line-breaks" will be \n normally with UNIX, and \r\n with Windows....
str_replace("\n", '</p><p>', $string)
well the line break thing is not working this is BEFORE I insert it in to the Query
//Run Sanitize Function $name = sanitize($_POST['name']); $date = ($_POST['date']); $e_mail = sanitize($_POST['e_mail']); $msg = sanitize($_POST['msg']); $msg = nl2br($msg); $web_site = sanitize($_POST['web_site']);
function sanitize($input) { if(get_magic_quotes_gpc()) { $input = stripslashes($input); } return(mysql_real_escape_string($input)); }
Sorry that is my sanitize function too