ShoeLace1291 Posted April 24, 2011 Share Posted April 24, 2011 I've never really made my own bbcode parser function before, but I'm going off of different tutorials to help me create my own style of parsing. I've actually never really used preg_replace much, either. My code is returning an odd error and I have no idea how to fix it. I'm using codeigniter and the error it returns is below: preg_replace() [function.preg-replace]: Compilation failed: nothing to repeat at offset 6 This is the code for my function: function bbParse($string){ $codes = array( '/\[b\](*?)\[\/b\]/' => '<b>\\1</b>', //bold tags '/\[h2\](*?)\[/h2\]/' => '<h2>\\1</h2>', //h2 tags '/\[h3\](*?)[\/h3\]/' => '<h3>\\1</h3>', //h3 tags '/\[quote\](*?)\[\/quote\]/' => '<blockquote>\\1</blockquote>', //quote tags using the template's blockquote tag '/\[img\](*?)\[/img\]/' => '<img src=\'\\1\' alt=\'Image Not Available\'>', //image tags '/\[url\=(*?)\](*?)\[/url\]/' => '<a href=\'\\1\' title=\'\\2\'>\\2</a>', //anchor tags '/\[p\](*?)\[/p\]/' => '<p>\\1</p>' //paragraph tags ); $string = preg_replace(array_keys($codes), array_values($codes), $string); return $string; } Quote Link to comment https://forums.phpfreaks.com/topic/234572-preg-replace-and-bb-codes/ Share on other sites More sharing options...
Fadion Posted April 24, 2011 Share Posted April 24, 2011 Try: '/\[b\](.+?)\[\/b\]/' => '<b>$1</b>', //bold tags .+? = every character (.), one or more (+), ungreedy (?). The $1 marks the matched characters replace part. Try the same for each key=>value part of your regex and it should work fine. Quote Link to comment https://forums.phpfreaks.com/topic/234572-preg-replace-and-bb-codes/#findComment-1205562 Share on other sites More sharing options...
ShoeLace1291 Posted April 26, 2011 Author Share Posted April 26, 2011 Now it's saying Compilation failed: missing terminating ] for character class at offset 18 Quote Link to comment https://forums.phpfreaks.com/topic/234572-preg-replace-and-bb-codes/#findComment-1206540 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.