Lodius2000 Posted August 7, 2008 Share Posted August 7, 2008 I am using nl2br and a custom str_replace function which should do the same thing as nl2br but as you can see neither works, check the code <?php //connect to db so mysql_real_escape_string works $string = "this is ' \" \ / a test"; print $string; //the above prints: // this is ' " \ / a test print "<br />\n"; $newstring = mysql_real_escape_string($string); print $newstring; //the above prints: // this\nis\n\'\n\"\n\\\n/\na\ntest print "<br />\n"; print nl2br($newstring); //the above prints: // this\nis\n\'\n\"\n\\\n/\na\ntest print "<br />\n"; $str = "Line 1\nLine 2\rLine 3\r\nLine 4\n"; $order = array("\r\n", "\n", "\r"); $replace = '<br />'; // Processes \r\n's first so they aren't converted twice. $newstr = str_replace($order, $replace, $newstring); print $newstr; //the above prints: // this\nis\n\'\n\"\n\\\n/\na\ntest print "<br />\n"; $str = "Line 1\nLine 2\rLine 3\r\nLine 4\n"; $order = array("\r\n", "\n", "\r"); $replace = '<br />'; // Processes \r\n's first so they aren't converted twice. $newstr2 = str_replace($order, $replace, $str); print $newstr2; //the above prints: // Line 1<br />Line 2<br />Line 3<br />Line 4<br /> print "<br />\n"; ?> Why? what can I do to fix it? thanks Quote Link to comment Share on other sites More sharing options...
moselkady Posted August 7, 2008 Share Posted August 7, 2008 You are using the variable $newstring in nl2br() and in your first str_replace(). $newstring is escaped and no longer has line-feed. You should use $string and $str instead: <?php print nl2br($string); .. .. $newstr = str_replace($order, $replace, $str); ?> Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted August 7, 2008 Share Posted August 7, 2008 new lines made in a text field are only indicated with \n. not \r. so don't include \r in your function Quote Link to comment Share on other sites More sharing options...
Lodius2000 Posted August 7, 2008 Author Share Posted August 7, 2008 my bad for not commenting off that first use of, $str it should be. but my point here is to show that no use of nl2br or my str_replace function will turn \r, or \n or \r\n into <br /> on a string escaped with mysql_real_escape_string is there another way that works because i cant figure it out what is particularly troubleing is, print nl2br($newstring); does not do what it is supposed to do, in fact it does nothing and that function is designed to convert data that has been escaped Quote Link to comment Share on other sites More sharing options...
Lodius2000 Posted August 7, 2008 Author Share Posted August 7, 2008 danny, as i understand it, new lines created in a text area on a windows OS use a \r\n, regardless the array $order should sort that out Quote Link to comment Share on other sites More sharing options...
moselkady Posted August 7, 2008 Share Posted August 7, 2008 The new-line character is escaped in $newstring due to mysql_real_escape_string(). Therefore $newstring doesn't have new-line character and nl2br($newstring) will return $newstring as it is. Try the un-escaped string: nl2br($string) Quote Link to comment Share on other sites More sharing options...
Lodius2000 Posted August 7, 2008 Author Share Posted August 7, 2008 ok so moselkady, that worked: "Try the un-escaped string: nl2br($string)" so now how do I un-escape the string that has been put through mysql_real_escape_string back into a string that looks like $string because strip slashes will leave a whole bunch of n's lying around Quote Link to comment Share on other sites More sharing options...
moselkady Posted August 7, 2008 Share Posted August 7, 2008 You can replace the string '\n' with the character "\n" <?php $replace = array("\r", "\n"); $order = array('\r', '\n'); $newstring = str_replace($order, $replace, $newstring); ?> Quote Link to comment Share on other sites More sharing options...
Lodius2000 Posted August 7, 2008 Author Share Posted August 7, 2008 now we are in business 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.