ChadNomad Posted March 7, 2009 Share Posted March 7, 2009 I am getting news from a column that's in BB Code. I want snippets as "news" but I'm having some problems. For example some code that comes out is [img=http://linktoimage.com] which I want to remove. Basically I would like to remove the tags and the text inside! Help appreciated, thanks. Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 7, 2009 Share Posted March 7, 2009 so what your current code, as bb is on goolge everywhere. example. i made a while back easy to alter. <?php /* * Prepare the rule set */ $arrayBBCode=array( ''=> array('type'=>BBCODE_TYPE_ROOT, 'childs'=>'!i'), 'b'=> array('type'=>BBCODE_TYPE_NOARG, 'open_tag'=>'<b>', 'close_tag'=>'</b>'), 'u'=> array('type'=>BBCODE_TYPE_NOARG, 'open_tag'=>'<u>', 'close_tag'=>'</u>', 'flags'=>BBCODE_FLAGS_SMILEYS_OFF), 'i'=> array('type'=>BBCODE_TYPE_NOARG, 'open_tag'=>'<i>', 'close_tag'=>'</i>', 'childs'=>'b'), ); /* * Parsed Text */ $text=<<<EOF [i] No parse Test [/i] [b] Parsed, with smiley [/b] [u] Parsed, with no smiley [/u] EOF; /* * Init the parser */ $BBHandler=bbcode_create($arrayBBCode); /* * Add Smiley rules to parser */ bbcode_add_smiley($BBHandler, "", "<img src=\"smiley.gif\" alt=\"\" />"); bbcode_add_smiley($BBHandler, "", "<img src=\"sad.gif\" alt=\"\" />"); bbcode_add_smiley($BBHandler, "", "<img src=\"happy.gif\" alt=\"\" />"); bbcode_add_smiley($BBHandler, "", "<img src=\"tong.gif\" alt=\"\" />"); bbcode_add_smiley($BBHandler, ":|", "<img src=\"special.gif\" alt=\":|\" />"); bbcode_add_smiley($BBHandler, ":6:", "<img src=\"six.gif\" alt=\":6:\" />"); /* * Parse the text */ echo bbcode_parse($BBHandler,$text); ?> Quote Link to comment Share on other sites More sharing options...
ChadNomad Posted March 7, 2009 Author Share Posted March 7, 2009 Sorry I have googled. I don't want to parse BB code, I want to remove everything within certain tags. So: [img=image] is completely removed. The tag, the content imbetween too. I appreciate the reply though. Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 7, 2009 Share Posted March 7, 2009 Look at this example, if i want to remove any think i alter the code. That the only way mate. <?php $var="[b]redarrow[/b]"; function AddBB($var) { $search = array( '/\[b\](.*?)\[\/b\]/is', // This line is regex bold. '/\[i\](.*?)\[\/i\]/is', '/\[u\](.*?)\[\/u\]/is', '/\[img\](.*?)\[\/img\]/is', '/\[url\](.*?)\[\/url\]/is', '/\[url\=(.*?)\](.*?)\[\/url\]/is' ); $replace = array( '<strong>$1</strong>', // this makes it bold when converted. '<em>$1</em>', '<u>$1</u>', '<img src="$1" />', '<a href="$1">$1</a>', '<a href="$1">$2</a>' ); $var = preg_replace ($search, $replace, $var); return $var; } echo ADDBB($var); ?> Quote Link to comment Share on other sites More sharing options...
ChadNomad Posted March 7, 2009 Author Share Posted March 7, 2009 Thanks for the help. That did it (I'm not any good with regular expressions). I ended up with this: $content = preg_replace('/\[img\](.*?)\[\/img\]/is', '', $r['body']); As it's the only tag I don't want parsed. Thanks Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 7, 2009 Share Posted March 7, 2009 i was thinking yee. that maybe you can use somthink like this as a clone to do the bb code as needed. not finished theo. <?php $var="[i]redarrow[/i]"; function AddBB($var) { $bold=('/\[b\](.*?)\[\/b\]/is'); $italic=('/\[i\](.*?)\[\/i\]/is'); $underline=('/\[u\](.*?)\[\/u\]/is'); $image=('/\[img\](.*?)\[\/img\]/is'); $url=('/\[url\](.*?)\[\/url\]/is'); $url2=('/\[url\=(.*?)\](.*?)\[\/url\]/is/'); $search = array( // un comment the bb you need. $bold, $italic, //$underline, //$image, //$url, //$url2 ); $replace = array( '<strong>$1</strong>', '<em>$1</em>', '<u>$1</u>', '<img src="$1" />', '<a href="$1">$1</a>', '<a href="$1">$2</a>' ); $var = preg_replace ($search, $replace, $var); return $var; } echo ADDBB($var); ?> 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.