Search code examples
phpregexphpbbphpbb3

Strip phpbb bbcode


I want to display the most recent posts from my phpbb3 forum on my website, but without the bbcode. so I'm trying to strip the bbcode but without succes one of the posts for example could be:

[quote="SimonLeBon":3pwalcod]bladie bla bla[/quote:3pwalcod]bla bla bladie bla blaffsd
fsdjhgfd dgfgdffgdfg

to strip bbcodes i use the function i found via google, I've tried several other similiar functions aswell:

 <?php
function stripBBCode($text_to_search) {
     $pattern = '|[[\/\!]*?[^\[\]]*?]|si';
     $replace = '';
     return preg_replace($pattern, $replace, $text_to_search);
}
?>

This however doesn't really have any effect.


Solution

  • Here's the one from phpBB (slightly adjusted to be standalone):

    /**
    * Strips all bbcode from a text and returns the plain content
    */
    function strip_bbcode(&$text, $uid = '')
    {
        if (!$uid)
        {
            $uid = '[0-9a-z]{5,}';
        }
    
        $text = preg_replace("#\[\/?[a-z0-9\*\+\-]+(?:=(?:&quot;.*&quot;|[^\]]*))?(?::[a-z])?(\:$uid)\]#", ' ', $text);
    
        $match = return array(
            '#<!\-\- e \-\-><a href="mailto:(.*?)">.*?</a><!\-\- e \-\->#',
            '#<!\-\- l \-\-><a (?:class="[\w-]+" )?href="(.*?)(?:(&amp;|\?)sid=[0-9a-f]{32})?">.*?</a><!\-\- l \-\->#',
            '#<!\-\- ([mw]) \-\-><a (?:class="[\w-]+" )?href="(.*?)">.*?</a><!\-\- \1 \-\->#',
            '#<!\-\- s(.*?) \-\-><img src="\{SMILIES_PATH\}\/.*? \/><!\-\- s\1 \-\->#',
            '#<!\-\- .*? \-\->#s',
            '#<.*?>#s',
        );
        $replace = array('\1', '\1', '\2', '\1', '', '');
    
        $text = preg_replace($match, $replace, $text);
    }