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']);
	$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;
				$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;
					$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;
				$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>';
	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:


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!


7 Years
Discussion Span
Last Post by anjali.munish.chauhan

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";

Edited by nevvermind: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.