PHP BBCode Module?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomprogers
Forum Commoner
Posts: 50
Joined: Fri Mar 17, 2006 5:17 pm
Location: Minnesota
Contact:

PHP BBCode Module?

Post 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.
Last edited by tomprogers on Thu May 04, 2006 1:35 pm, edited 2 times in total.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post 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.
tomprogers
Forum Commoner
Posts: 50
Joined: Fri Mar 17, 2006 5:17 pm
Location: Minnesota
Contact:

Post by tomprogers »

Wow, thanks. I had never noticed the array usage of preg_replace, either.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

no problem. i very recently had to do this myself. i.e. just the other day ;)
Post Reply