Writing a template parser for BB Code

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
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Writing a template parser for BB Code

Post by Jim »

I just can't seem to figure out template parsing. How do forums like this change

Code: Select all

їurl=http://site.com]Site dot Comї/url]
In to a working link for site.com? I can't find tuts for this anywhere!
ghost007
Forum Commoner
Posts: 49
Joined: Sat Nov 22, 2003 10:10 am

just copy it from existing open source bb board :)

Post by ghost007 »

Code: Select all

<?php
/**
 * bbdecode/bbencode functions:
 * Rewritten - Nathan Codding - Aug 24, 2000
 * quote, code, and list rewritten again in Jan. 2001.
 * All BBCode tags now implemented. Nesting and multiple occurances should be 
 * handled fine for all of them. Using str_replace() instead of regexps often
 * for efficiency. quote, list, and code are not regular, so they are 
 * implemented as PDAs - probably not all that efficient, but that's the way it is. 
 *
 * Note: all BBCode tags are case-insensitive.
 */

function bbencode($message, $is_html_disabled) {

	// pad it with a space so we can distinguish between FALSE and matching the 1st char (index 0).
	// This is important; bbencode_quote(), bbencode_list(), and bbencode_code() all depend on it.
	$message = " " . $message;
	
	// First: If there isn't a "[" and a "]" in the message, don't bother.
	if (! (strpos($message, "[") && strpos($message, "]")) )
	{
		// Remove padding, return.
		$message = substr($message, 1);
		return $message;	
	}

	//

Code: Select all

and
for posting code (HTML, PHP, C etc etc) in your posts.
$message = bbencode_code($message, $is_html_disabled);

//
and
for posting replies with quote, or just for quoting stuff.
$message = bbencode_quote($message);

//
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Or str_replace (preferred to regex, unless you really need to).
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

replace with <a href= and then ] with > then with </a>

I used [php_man]eregi_replace[/php_man]() for my forum's BBcode. =)
Joan Garnet
Forum Newbie
Posts: 19
Joined: Fri Jun 20, 2003 9:56 am
Location: Barcelona

Post by Joan Garnet »

I'm just doing something like that :D
regular expressions are the key, but it's not that easy ...
Post Reply