Page 1 of 1

PHP BBCode Module?

Posted: Wed May 03, 2006 5:20 pm
by tomprogers
Is there some kind of module or function library I can include to support bbcode in select instances? I'm not looking for any sort of fancy phpBB template formatting. Essentially, I want to be able to do this:

Code: Select all

$formattedAnswer = bbCodify($rawAnswer);
Some basic support for lists, bold and italicized text, links, and things like that.

Right now I'm sorting through the full release of phpBB to see how much work is involved in adapting the bbcode.php and tpl file, and it's looking like several hours. If possible, I'd like to avoid that.

Posted: Wed May 03, 2006 5:24 pm
by Charles256

Code: Select all

function bbcode_format ($str) 
	{
    	$str = htmlentities($str);

	    $simple_search = array(
                '/\[b\](.*?)\[\/b\]/is',                                
                '/\[i\](.*?)\[\/i\]/is',                                
                '/\[u\](.*?)\[\/u\]/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',        
                );

	    $simple_replace = array(
                '<strong>$1</strong>',
                '<em>$1</em>',
                '<u>$1</u>',
                '<a href="$1">$2</a>',
                '<a href="$1">$1</a>',
                '<div style="text-align: $1;">$2</div>',
                '<img src="$1" />',
                '<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>',
                );

	    // Do simple BBCode's
	   	$str = preg_replace($simple_search, $simple_replace, $str);

	    // Do <blockquote> BBCode
	    $str = bbcode::bbcode_quote($str);

	    return $str;
	}



	function bbcode_quote ($str) 
	{
	    $open = '<blockquote>';
	    $close = '</blockquote>';

	    // How often is the open tag?
	    preg_match_all ('/\[quote\]/i', $str, $matches);
	    $opentags = count($matches['0']);
	
	    // How often is the close tag?
	    preg_match_all ('/\[\/quote\]/i', $str, $matches);
	    $closetags = count($matches['0']);
	
	    // Check how many tags have been unclosed
	    // And add the unclosing tag at the end of the message
	    $unclosed = $opentags - $closetags;
	    for ($i = 0; $i < $unclosed; $i++) 
		{
	        $str .= '</blockquote>';
	    }

	    // Do replacement
	    $str = str_replace ('[' . 'quote]', $open, $str);
	    $str = str_replace ('[/' . 'quote]', $close, $str);

	    return $str;
	}
may need to mod.

Posted: Wed May 03, 2006 6:26 pm
by tomprogers
Wow, thanks. I had never noticed the array usage of preg_replace, either.

Posted: Wed May 03, 2006 6:54 pm
by Charles256
no problem. i very recently had to do this myself. i.e. just the other day ;)