kaiman Posted January 6, 2011 Share Posted January 6, 2011 Hi all, I am working on debugging a function that converts single breaks in a form text area field into HTML break tags and double breaks into paragraph tags before sticking them into a MySQL DB, so that they can be retrieved and properly displayed on another page. The script seems to work fine for the most part except it seems to be trimming the text around the break tags off. My assumption is in the way that I am trimming the string, but I am not sure. Here is my code: <?php // turns line breaks in forms into HTML <br> <br/> or <p></p> tags function nl2p($string, $line_breaks = true, $xml = true) { // remove existing HTML formatting to avoid double tags $string = str_replace(array('<p>', '</p>', '<br>', '<br/>'), '', $string); // convert single line breaks into <br> or <br/> tags if ($line_breaks == true) return '<p>'.preg_replace(array("/([\n]{2,})/i", "/([^>])\n([^<])/i"), array("</p>\n<p>", '<br'.($xml == true ? '/' : '').'>'), trim($string)).'</p>'; // convert double line breaks into <p></p> tags else return '<p>'.preg_replace("/([\n]{1,})/i", "</p>\n<p>", trim($string)).'</p>'; } ?> <?php // example uses $text = "Hello. My name is Kai."; echo nl2p($text) . "\n"; $text = "Your IP is: {$_SERVER["REMOTE_ADDR"]}"; echo nl2p($text) . "\n"; ?> Here is what it is returning: <p>Hello.</p> <p>My name is Kai.</p> <p>Your IP is<br/>23.123.123.123</p> And here is what it should be returning (notice the missing : and first number of the IP address in the code above): <p>Hello.</p> <p>My name is Kai.</p> <p>Your IP is:<br/>123.123.123.123</p> Also, I remember reading somewhere about Mac, Linux, and Windows OS using different break tags could this have something to do with it (I am on Mac OS X), and how would I implement that into this script. Any help or suggestions are appreciated. Thanks, kaiman Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted January 6, 2011 Share Posted January 6, 2011 1. i suspect this pattern is removing the preceding and following characters: "/([^>])\n([^<])/i" As you replaced double \n in the first pattern, can't you simply replace single \n in the second pattern? 2. Mac is essentially unix. mac/unix/linux line breaks are \n while windows is often \r\n. i guess i would replace all \r with nothing before performing the search for \n Quote Link to comment Share on other sites More sharing options...
kaiman Posted January 6, 2011 Author Share Posted January 6, 2011 @ BlueSkyIS Maybe I'm a bit dense but I'm not sure I am following you on your first point. Can you elaborate with an example? BTW, thanks for the info on the OS line carriage differences, I thought it was something like that. kaiman Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted January 6, 2011 Share Posted January 6, 2011 this might work for you. function nl2p($string, $line_breaks = true, $xml = true) { // remove existing HTML formatting to avoid double tags $string = str_replace(array('<p>', '</p>', '<br>', '<br/>'), '', $string); // convert single line breaks into <br> or <br/> tags if ($line_breaks == true) { return '<p>'.preg_replace(array("/\r/","/\n{2,}/", "/\n/"), array('','</p><p>', '<br'.($xml == true ? '/' : '').'>'), $string).'</p>'; } else { return '<p>'.preg_replace("/\n/", "</p>\n<p>", trim($string)).'</p>'; } } Quote Link to comment Share on other sites More sharing options...
3raser Posted January 7, 2011 Share Posted January 7, 2011 That isn't your IP is it? I wouldn't show off your IP like that. :/ 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.