rickyj Posted August 26, 2007 Share Posted August 26, 2007 Hi there, I’m having a real bit of trouble, and I cant narrow it down. I’m trying to get a form to post a message to a mySQL data base, and replace the users input new lines into <br> - But this doesn’t seem to be working as expected. - I have tried lots of things, but they all give strange results. - the Mysql column is a text column - I have tried using nl2br, but this does nothing to the text. Im currently using: function nl2br2($string) { $string = str_replace('\r\n', '<br>', $string); $string = str_replace('\r', '<br>', $string); $string = str_replace('\n', '', $string); return $string; This works perfect, unless the new line start with an n, if the new line starts with an n the n gets striped: first line new line third gets converted to first line<br>ew line<br>third if I use this: function nl2br2($string) { $string = str_replace('\r\n', '<br>', $string); $string = str_replace('\r', '<br>', $string); return $string; I get: first line \nnew line \nthird I cant figure out whets going on here? I have also tried using variations, but get exactly the same result: function nl2br2($string) { $string = str_replace('\n\r\n', '<br>', $string); $string = str_replace('\r\n\n', '<br>', $string); $string = str_replace('\r\n', '<br>', $string); $string = str_replace('\r', '<br>', $string); $string = str_replace('\\n', '', $string); $string = str_replace('\nn', ' n', $string); $string = str_replace('\n', '', $string); return $string; gets converted to first line<br>ew line<br>third function nl2br2($string) { $string = str_replace('\n\r\n', '<br>', $string); $string = str_replace('\r\n\n', '<br>', $string); $string = str_replace('\r\n', '<br>', $string); $string = str_replace('\r', '<br>', $string); $string = str_replace('\\n', '', $string); $string = str_replace('\nn', ' n', $string); return $string; I get: first line \nnew line \nthird Any on know what is happening? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted August 26, 2007 Share Posted August 26, 2007 When using escape characters use double quotes and not single quotes. However you may aswell use nl2br to do what you are trying to accomplish. However only use nl2br when you get the data out of the database not when you insert the data into the database. Quote Link to comment Share on other sites More sharing options...
graham23s Posted August 26, 2007 Share Posted August 26, 2007 Don't know if this is any help mate, but a while back a forum member wrote this function which iuse regularly: function CleanPosts($String, $nlbr = false) { if (get_magic_quotes_gpc()) { $String = stripslashes($String); } if ($nlbr) { $String = nl2br($String); } works great:) Graham Quote Link to comment Share on other sites More sharing options...
MadTechie Posted August 26, 2007 Share Posted August 26, 2007 try this $string= preg_replace('/\r\n|\r|\n/', '<br>', $string); Quote Link to comment Share on other sites More sharing options...
rickyj Posted August 26, 2007 Author Share Posted August 26, 2007 try this $string= preg_replace('/\r\n|\r|\n/', '<br>', $string); Thanks for the help, I tried this but get the following for: 1st line new line third line gets converted to :1st line\r\nnew line\r\nthird line\r\n (looking at the database: looks exactly the same) i.e., it doesn’t get converted at all. yet, $string = preg_replace('\r', '<br>', $string); works but leave /n at the start of each line, and $string = preg_replace('\r\n', '<br>', $string); works, but deletes n's off of the start of a new line if its present <-- this I find really odd! Quote Link to comment Share on other sites More sharing options...
rickyj Posted August 26, 2007 Author Share Posted August 26, 2007 my above code was supposed to say str_replace (The edit function was not working for me?). preg replace doesnt seem to work here for some reason? However, It should be noted, before I use these variables, I get them via this method: stripslashes((get_magic_quotes_gpc()) ? $_POST[$MyString] : addslashes($_POST[$MyString]))); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.