I'm working on a site that on the homepage it rips posts off of a certain forum in the forums... That works perfectly. I am now trying to parse bbcode from the posts.

function BBCode ($string) {
	global $globals;
	$search = array();
	$replace = array();
	$con = mysql_connect($globals['forum']['host'].':'.$globals['forum']['port'],$globals['forum']['user'],$globals['forum']['pass']);
	mysql_select_db($globals['forum']['db'],$con);
	$sql = "SELECT `bbcode_tag`,`bbcode_replace`,`bbcode_useoption`,`bbcode_single_tag`,`bbcode_switch_option` FROM `custom_bbcode` WHERE `bbcode_sections`='all'";
	$result = mysql_query($sql);
	$base_url = $globals['main']['url'].$globals['forum']['path'];
	$i = 0;
	echo '<table><td>';
	while ($row = mysql_fetch_array($result)){
		if ($row['bbcode_single_tag']==1){
			if ($row['bbcode_useoption']==1){
				$search[$i] = "/[".$row['bbcode_tag']."\=(.*?)\]/is";
				$rep = $row['bbcode_replace'];
				$rep = str_replace("{base_url}", $base_url, $rep);
				$rep = str_replace("{option}", "\\1", $rep);
				$replace[$i] = $rep;
			}
			else{
				$search[$i] = "/[".$row['bbcode_tag']."\]/is";
				$rep = $row['bbcode_replace'];
				$rep = str_replace("{base_url}", $base_url, $rep);
				$replace[$i] = $rep;
			}
		}
		else {
			if ($row['bbcode_useoption']==1){
				if ($row['bbcode_switch_option']==1){
					$search[$i] = "/[".$row['bbcode_tag']."\](.*?)\[\/".$row['bbcode_tag']."\=(.*?)\]/is";
					$rep = $row['bbcode_replace'];
					$rep = str_replace("{base_url}", $base_url, $rep);
					$rep = str_replace("{content}", "\\1", $rep);
					$rep = str_replace("{option}", "\\2", $rep);
					$replace[$i] = $rep;
				}
				else{
					$search[$i] = "/[".$row['bbcode_tag']."\=(.*?)\](.*?)\[\/".$row['bbcode_tag']."\]/is";
					$rep = $row['bbcode_replace'];
					$rep = str_replace("{base_url}", $base_url, $rep);
					$rep = str_replace("{content}", "\\2", $rep);
					$rep = str_replace("{option}", "\\1", $rep);
					$replace[$i] = $rep;
				}
			}
			else{
				$search[$i] = "/[".$row['bbcode_tag']."\](.*?)\[\/".$row['bbcode_tag']."\]/is";
				$rep = $row['bbcode_replace'];
				$rep = str_replace("{base_url}", $base_url, $rep);
				$rep = str_replace("{content}", "\\1", $rep);
				$replace[$i] = $rep;
			}
		}
		echo $search[$i].'</td><tr /><td>';
		$i++;
	}
	echo '</td></table>';
	return preg_replace($search, $replace, $string);
}

When I run this function on the post it gives me this error.

Warning: preg_replace() [function.preg-replace]: Compilation failed: missing terminating ] for character class at offset 30 in *****/functions.php on line 2920

I put in the echo functions so I could track what the @#!% the code was putting out as it read from the mysql database...

this is what the echo puts out:

/[snapback\](.*?)\[\/snapback\]/is
/[right\](.*?)\[\/right\]/is
/[left\](.*?)\[\/left\]/is
/[center\](.*?)\[\/center\]/is
/[topic\=(.*?)\](.*?)\[\/topic\]/is
/[post\=(.*?)\](.*?)\[\/post\]/is
/[spoiler\](.*?)\[\/spoiler\]/is
/[acronym\=(.*?)\](.*?)\[\/acronym\]/is
/[b\](.*?)\[\/b\]/is
/[i\](.*?)\[\/i\]/is
/[u\](.*?)\[\/u\]/is
/[hr\]/is
/[code\](.*?)\[\/code\]/is
/[php\](.*?)\[\/php\]/is
/[html\](.*?)\[\/html\]/is
/[sql\](.*?)\[\/sql\]/is
/[xml\](.*?)\[\/xml\]/is
/[url\=(.*?)\](.*?)\[\/url\]/is
/[img\](.*?)\[\/img\]/is
/[quote\](.*?)\[\/quote\]/is
/[indent\](.*?)\[\/indent\]/is
/[list\](.*?)\[\/list\]/is
/[strike\](.*?)\[\/strike\]/is
/[sub\](.*?)\[\/sub\]/is
/[sup\](.*?)\[\/sup\]/is
/[email\=(.*?)\](.*?)\[\/email\]/is
/[background\=(.*?)\](.*?)\[\/background\]/is
/[color\=(.*?)\](.*?)\[\/color\]/is
/[size\=(.*?)\](.*?)\[\/size\]/is
/[font\=(.*?)\](.*?)\[\/font\]/is
/[member\=(.*?)\]/is
/[media\=(.*?)\](.*?)\[\/media\]/is
/[extract\](.*?)\[\/extract\]/is
/[blog\=(.*?)\](.*?)\[\/blog\]/is
/[entry\=(.*?)\](.*?)\[\/entry\]/is
/[twitter\](.*?)\[\/twitter\]/is
/[tab\]/is

Hopefully someone here understands what is going on and why. I for the life of me cannot figure this out...

Any help would be oh so welcome!

Thanks

Member Avatar for nevvermind

So which one's line 2920?

Later edit.
Never mind. Here's what I think.
You never escaped the initial bracket. So PHP thinks you want a character class.

// this...
$search[$i] = "/[".$row['bbcode_tag']."\=(.*?)\]/is";

// ... should be this:
$search[$i] = "/\[".$row['bbcode_tag']."\=(.*?)\]/is";

If this is the case, you've got a lot of editing to do. :P
When working with tags (be it html, xml or bbcode or w\e), use a delimiter to clearly see the slashes and back-slashes and, of course, this means that you needn't be escaping all the slashes (cleaner regex). I use this:

$search[$i] = "#\[".$row['bbcode_tag']."\=(.*?)\]#is";

Your final suggestion works perfectly... thank you so much!

Member Avatar for nevvermind

Mark this thread as solved, pls.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.