Page 1 of 1

Writing a template parser for BB Code

Posted: Thu Nov 27, 2003 12:44 pm
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!

just copy it from existing open source bb board :)

Posted: Thu Nov 27, 2003 1:29 pm
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);

//

Posted: Thu Nov 27, 2003 5:52 pm
by McGruff
Or str_replace (preferred to regex, unless you really need to).

Posted: Thu Nov 27, 2003 7:22 pm
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. =)

Posted: Thu Nov 27, 2003 8:06 pm
by Joan Garnet
I'm just doing something like that :D
regular expressions are the key, but it's not that easy ...