Grego Posted December 15, 2007 Share Posted December 15, 2007 I've stored all my BBcode terms in a MySQL database containing, amongst other things, their patterns and replacements. For example: Pattern: /\[b\](*.?)\[\/b\]/is Replacement: <strong>$1</strong> There are five of these records: b, i, u, url, img I then have a piece of code which draws out the patterns and replacements and puts them in an array. I use preg_replace to replace all the patterns with their respective replacements. For some reason, I'm getting an error saying: "Warning: preg_replace() [function.preg-replace]: Compilation failed: nothing to repeat at offset 6 in /home/commande/public_html/gregoland/functions_bbcode.php on line 20". I've tried googling this but have only returned examples of it rather than solutions. Does anybody know what's wrong and how I can get rid of this error? This is the code I'm using: $bb_codes_q = $db->execute('SELECT * FROM `bbcode`'); $bb_patterns = array(); $bb_replaces = array(); while($bb_codes = $bb_codes_q->fetchrow()){ array_push($bb_patterns,$bb_codes['pattern']); array_push($bb_replaces,$bb_codes['replace']); } $bb_text = preg_replace($bb_patterns,$bb_replaces,$bb_text); return $bb_text; Quote Link to comment https://forums.phpfreaks.com/topic/81793-solved-bbcode-parsing-problems-preg_replace/ Share on other sites More sharing options...
rab Posted December 15, 2007 Share Posted December 15, 2007 Try a print_r() for both arrays before you do the preg_replace(). Maybe there is an error in your regexp. Quote Link to comment https://forums.phpfreaks.com/topic/81793-solved-bbcode-parsing-problems-preg_replace/#findComment-415627 Share on other sites More sharing options...
Grego Posted December 16, 2007 Author Share Posted December 16, 2007 I've tried that and I can't see anything wrong with either array. I tried too to replace the arrays with an empty "array()". When I replaced $bbcode_patterns, I found that the error had gone. So I've narrowed it down to that variable being wrong. Here is the print_r for it: Array ( [0] => /\[b\](*.?)\[\/b\]/is [1] => /\[i\](*.?)\[\/i\]/is [2] => /\[u\](*.?)\[\/u\]/is [3] => /\[url\=(*.?)\](*.?)\[\/url\]/is [4] => /\[img\](*.?)\[\/img\]/is ) Quote Link to comment https://forums.phpfreaks.com/topic/81793-solved-bbcode-parsing-problems-preg_replace/#findComment-416120 Share on other sites More sharing options...
The Little Guy Posted December 16, 2007 Share Posted December 16, 2007 I don't know If this will help or not, maybe it will be easier??? but this is what I use for bbc: http://phpsnips.com/snippet.php?id=41 Quote Link to comment https://forums.phpfreaks.com/topic/81793-solved-bbcode-parsing-problems-preg_replace/#findComment-416126 Share on other sites More sharing options...
corbin Posted December 16, 2007 Share Posted December 16, 2007 /\[b\](*.?)\[\/b\]/is That's an invalid pattern x.x * means number of occurances >= 0 and . means any character, so I think you mean .*, not *.. Quote Link to comment https://forums.phpfreaks.com/topic/81793-solved-bbcode-parsing-problems-preg_replace/#findComment-416127 Share on other sites More sharing options...
Grego Posted December 16, 2007 Author Share Posted December 16, 2007 You're right Corbin - though I figured it out before you said I replaced all the (*.?)'s with (.*) and it works fine. A really stupid error. :-\ But thanks to all anyway. Quote Link to comment https://forums.phpfreaks.com/topic/81793-solved-bbcode-parsing-problems-preg_replace/#findComment-416130 Share on other sites More sharing options...
corbin Posted December 16, 2007 Share Posted December 16, 2007 That's also what the error meant since * meant repeat, and there was nothing before it ;p. Quote Link to comment https://forums.phpfreaks.com/topic/81793-solved-bbcode-parsing-problems-preg_replace/#findComment-416131 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.