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.