Hi

Can anyone tell me why this isn't working:

function formatbb($bbcontent) {
	$bbcontent = htmlspecialchars($bbcontent);
	$sql = mysql_query("SELECT * FROM bbcodes ORDER BY id ASC") or die("Cannot get codes: ".mysql_error());
	$i = 0;
	while($patterns = mysql_fetch_array($sql)) {
		if($patterns['type'] == '1') {
			$pattern[$i] = "|\[".$patterns['code']."\](.*?)\[/".$patterns['code']."\]|s";
			$replacement[$i] = $patterns['replacement'];
		} else if($patterns['type'] == '2') {
			$pattern[$i] = "|\[".$patterns['code']."=(.*?)\](.*?)\[/".$patterns['code']."\]|s";
			$replacement[$i] = $patterns['replacement'];
		} else if($patterns['type'] == '3') {
			$pattern[$i] = "|\[".$patterns['code']."\]|s";
			$replacement[$i] = $patterns['replacement'];
		} else {
			$pattern[$i] = "|\:".$patterns['code']."\:|s";
			$replacement[$i] = $patterns['replacement'];
		}
		$i++;
	}
	$bbcontent = preg_replace($pattern, $replacement, $bbcontent); 
	return $bbcontent;
}

it works on my local machine but not on my web server (see what happens at: http://www.treble.lfhost.com/1024/pages.php?page=download&task=detail&id=9

    Apparently, you need to be registered on the treble site to see the broken page... so no, I can't see what's broken.

    Are you getting any error messages?
    Are you sure that the bbcodes table exists on the web server?
    Are you sure you're connecting to the database?
    If so, are you sure that the database on the server has the same data as your local database?
    Are you sure that $bbcontent has a value when it's passed to your function?
    (If you have register globals turned off, your script might not have immediate access to $bbcontent).

    The code looks ok. You should put in some troubleshooting to see what's coming out of the database and what's going into the function.

      sorry, save you going to the website the error given is:

      Warning: preg_replace(): Compilation failed: unmatched parentheses at offset 3 in /home/concept/public_html/1024/library/bbcode.php on line 109

      Warning: preg_replace(): Compilation failed: unmatched parentheses at offset 3 in /home/concept/public_html/1024/library/bbcode.php on line 109

      Warning: preg_replace(): Compilation failed: missing ) at offset 6 in /home/concept/public_html/1024/library/bbcode.php on line 109

      line 109 is: $bbcontent = preg_replace($pattern, $replacement, $bbcontent);

      edit: i've turned off registration for the downloads page so you will be able to get in there now. the code works fine (if you look you will see the bold text has been, well bolded). i think it's something to do with php versions but im not sure

        Insert a line just before line 109 that looks like this:

        print "Pattern: $pattern<br>Replacement: $replacement<br>BBContent: $bbcontent<p>";

        This way, you will be able to see what preg_replace is trying to work with. As I said, since I can't see in your database, I have no way to know if the values in your local database are the same as the values of your web site's database.

        A mismatched parenthesis looks like this:

        the (quick) brown (fox (jumped) (over the (lazy) dog)

        Notice that there are more open parentheses than close parentheses. And that doesn't work - it doesn't make sense.

        So your error is basically that the data you're getting from the database isn't written correctly.

          hmm i can't see anything in the database. the patterns print out as:

          |[hr]|s
          |[quote](.?)[/quote]|s
          |[url](.
          ?)[/url]|s
          |[b](.?)[/b]|s
          |[i](.
          ?)[/i]|s
          |[center](.?)[/center]|s
          |[email=(.
          ?)](.?)[/email]|s
          |[right](.
          ?)[/right]|s
          |[code](.?)[/code]|s
          |[url=(.
          ?)](.?)[/url]|s
          |[email=(.
          ?)](.?)[/email]|s
          |[img](.
          ?)[/img]|s
          |:🙂:|s
          |::hug::|s
          |:🙁:|s
          |::shy::|s
          |::p:|s
          |[u](.*?)[/u]|s

          i've also attached the sql. i think it is something to do with the type in the if statements, not matching correctly?

            Sorry, my mistake. The variable $pattern isn't coming from the database - you're building it in the code - so the problem isn't in the database.

            Look at some of your patterns. This one for example:

            |\::)\:|s

            appears to have more closing parentheses than open parentheses. The error message you're getting is saying "unmatched parentheses".

            I suspect that the installation of PHP on one machine is behaving differently than PHP on the other machine. Maybe your local machine has errors turned off? Or it's a different version of PHP where preg_match behaves differently?

            Whatever the cause, at least you know where the problem is now.

              ok it's definately the last expression but im having troubles with the delimiter and getting the pattern to load properly.....help :-\

                2 months later

                help! i'm still waiting on s fix for this:

                function formatbb ($bbcontent) {
                	global $themedir;
                	$sql = mysql_query("SELECT * FROM bbcodes ORDER BY id ASC") or die("Cannot get codes: ".mysql_error());
                	$i = 0;
                	while($patterns = mysql_fetch_array($sql)) {
                		if($patterns['type'] == '1') { //Somethiing like [url]http://www.somewhere.com[/url]
                			$pattern[$i] = "|\[".$patterns['code']."\](.*?)\[/".$patterns['code']."\]|s";
                			$replacement[$i] = $patterns['replacement'];
                		} else if($patterns['type'] == '2') { //Something like [url=http://www.somewhere.com]Click Here![/url]
                			$pattern[$i] = "|\[".$patterns['code']."=(.*?)\](.*?)\[/".$patterns['code']."\]|s";
                			$replacement[$i] = $patterns['replacement'];
                		} else if($patterns['type'] == '3') { // [hr]
                			$pattern[$i] = "|\[".$patterns['code']."\]|s";
                			$replacement[$i] = $patterns['replacement'];
                		} else if($patterns['type'] == '4') { //Smiley!
                			$pattern[$i] = "|".$patterns['code']."|s";
                			$replacement[$i] = $patterns['replacement'];
                		}
                		$i++;
                	}
                	$bbcontent = preg_replace($pattern, $replacement, $bbcontent);
                	return $bbcontent;
                }
                

                throws:

                Warning: Compilation failed: unmatched parentheses at offset 1 in c:\documents and settings\ashley\my documents\treble designs\library\config.php on line 237
                
                Warning: Compilation failed: unmatched parentheses at offset 1 in c:\documents and settings\ashley\my documents\treble designs\library\config.php on line 237
                
                Warning: Compilation failed: missing ) at offset 2 in c:\documents and settings\ashley\my documents\treble designs\library\config.php on line 237

                it's the smiley line but i can't figure out how to get it to work

                  The error message, as well as my previous posts, tell you what the problem is. You have unmatched parentheses in your patterns. You need to escape parentheses that aren't being using to capture parts of the pattern.

                    Write a Reply...