brady123 Posted November 2, 2011 Share Posted November 2, 2011 I currently have some custom tags, similar to BBCode, to be used for comments on my site. I'm trying to create a [nobr] tag (note: I know the no break tag will work in many major browsers, but it's deprecated, so I'm trying to be compliant) to remove all line breaks between the opening and closing tag. The idea is that if I encounter the [nobr] tag, I need to remove all line breaks between the opening and closing tags. Here's the best I could come up with, but it doesn't work. I can't think of anything else! Can anyone shed some light and help me out. I could be way off here... Note: the new part I'm working on is where you see the before, during, after. The other part of the function is for reference of what I'm working with thus far. function BbToHtml($str) { $pattern = array ( "/\[b\](.+)\[\/b\]/Usi", "/\[i\](.+)\[\/i\]/Usi", "/\[u\](.+)\[\/u\]/Usi", "/\[color=(\#[0-9A-F]{6}|[a-z]+)\](.*)\[\/color\]/Usi", "/\[size=(.*)\](.*)\[\/size\]/Usi", "/\[img=(.*)\](.*)\[\/img\]/Usi", "/\[url=(.*)\](.+)\[\/url\]/Usi", ); $replace = array ( "<strong>\\1</strong>", "<em>\\1</em>", "<u>\\1</u>", "<span style=\"color:\\1\">\\2</span>", "<img src='\\1' alt='\\2' border=\"0\"/>", "<a href='\\1' target='_new'>\\2</a>", ); $before=preg_replace("(.*)/\[nobr\](.*)\[\/nobr](.*)/Usie", "\\1", $str); $during=preg_replace("(.*)/\[nobr\](.*)\[\/nobr](.*)/Usie", "\\2", $str); $after=preg_replace("(.*)/\[nobr\](.*)\[\/nobr](.*)/Usie", "\\3", $str); if($during != "") { $during=str_replace("\r\n","",$during); $str=$before.$during.$after; } $str=nl2br($str); $str = preg_replace($pattern,$replace,$str); return $str; } Quote Link to comment https://forums.phpfreaks.com/topic/250330-preg_replace-to-remove-line-breaks-between-two-tags/ Share on other sites More sharing options...
brady123 Posted November 3, 2011 Author Share Posted November 3, 2011 Dug in and figured it out...whew. For any poor souls out there: Make sure to do this AFTER the nl2br function. $str = preg_replace("/\[nobr\](.*?)\[\/nobr\]/esiU", "stripslashes(preg_replace('/<br(\s)?(?(1)\/)>/i', '', '\\1'))", $str); Quote Link to comment https://forums.phpfreaks.com/topic/250330-preg_replace-to-remove-line-breaks-between-two-tags/#findComment-1284478 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.