pianoman993 Posted February 8, 2009 Share Posted February 8, 2009 Hello there PHP experts! I recently posted a similar post about decoding bbcode. In this question I am wondering why I keep getting an extra <br /> tag after every set of [ center ] [ / center ] tags. Here is my code! function bb_encode ($str) { $str = nl2br(htmlentities($str)); $simple_search = array( //added line break '/\[b\](.*?)\[\/b\]/is', '/\[i\](.*?)\[\/i\]/is', '/\[u\](.*?)\[\/u\]/is', '/\[s\](.*?)\[\/s\]/is', '/\[center\](.*?)\[\/center\]/is', '/\[url\=(.*?)\](.*?)\[\/url\]/is', '/\[url\](.*?)\[\/url\]/is', '/\[align\=(left|center|right)\](.*?)\[\/align\]/is', '/\[img\](.*?)\[\/img\]/is', '/\[mail\=(.*?)\](.*?)\[\/mail\]/is', '/\[mail\](.*?)\[\/mail\]/is', '/\[font\=(.*?)\](.*?)\[\/font\]/is', '/\[size\=(.*?)\](.*?)\[\/size\]/is', '/\[color\=(.*?)\](.*?)\[\/color\]/is', //added textarea for code presentation '/\[codearea\](.*?)\[\/codearea\]/is', //added pre class for code presentation '/\[code\](.*?)\[\/code\]/is', //added paragraph '/\[p\](.*?)\[\/p\]/is', ); $simple_replace = array( //added line break '<strong>$1</strong>', '<em>$1</em>', '<u>$1</u>', '<s>$1</s>', '<div style="text-align:center">$1</div>', // added nofollow to prevent spam '<a href="$1" rel="nofollow" title="$2 - $1">$2</a>', '<a href="$1" rel="nofollow" title="$1">$1</a>', '<div style="text-align: $1;">$2</div>', //added alt attribute for validation '<img src="$1" alt="" />', '<a href="mailto:$1">$2</a>', '<a href="mailto:$1">$1</a>', '<span style="font-family: $1;">$2</span>', '<span style="font-size: $1;">$2</span>', '<span style="color: $1;">$2</span>', //added textarea for code presentation '<textarea class="code_container" rows="30" cols="70">$1</textarea>', //added pre class for code presentation '<pre class="code">$1</pre>', //added paragraph '<p>$1</p>', ); // Do simple BBCode's $str = preg_replace ($simple_search, $simple_replace, $str); return $str; } function bb_decode ($str) { $str = ($str); $bbcode = array( "/\<strong>(.*?)<\/strong>/is" => "[b]$1[/b]", "/\<em>(.*?)<\/em>/is" => "[i]$1[/i]", "/\<u>(.*?)<\/u>/is" => "[u]$1[/u]", "/\<s>(.*?)<\/s>/is" => "[s]$1[/s]", "/\<div style=\"text-align:center\">(.*?)<\/div>/is" => " [center]$1[/center] ", "/\<br \/>/is" => "" ); $str = preg_replace(array_keys($bbcode), array_values($bbcode), $str); return $str; } Any help would be greatly appreciated! Thanks! - Pianoman993 Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted February 8, 2009 Share Posted February 8, 2009 Well, you are calling nl2br() which replaces all line breaks with HTML line break tags, so if you have a line break after your center bbcode then you'll get an HTML line break. Quote Link to comment Share on other sites More sharing options...
pianoman993 Posted February 8, 2009 Author Share Posted February 8, 2009 How then can I detect line breaks without creating <br /> tags? 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.