RobertP Posted June 20, 2012 Share Posted June 20, 2012 why is this not working? http://gskinner.com/RegExr/?31a9l preg_replace(): Unknown modifier 'b' <?php $string = 'This is a [b]simple BBCode[/b] test. This is another test [b]line[/b]. [b]tt[/b]'; $string = preg_replace('/\[b](.*)\[/b]/gi','<span style="font-size:bold;">$1</span>',$string); echo str_replace("\n",'<br />',$string); ?> Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted June 20, 2012 Share Posted June 20, 2012 change [/b] to [\/b] Quote Link to comment Share on other sites More sharing options...
xyph Posted June 20, 2012 Share Posted June 20, 2012 A 'better' solution would be to use a different delimiter. Escaping delimiters within the expression when you don't have to just adds complexity. http://php.net/manual/en/regexp.reference.delimiters.php '~\[b](.*)\[/b]~gi' Quote Link to comment Share on other sites More sharing options...
RobertP Posted June 20, 2012 Author Share Posted June 20, 2012 A 'better' solution would be to use a different delimiter. Escaping delimiters within the expression when you don't have to just adds complexity. http://php.net/manual/en/regexp.reference.delimiters.php '~\[b](.*)\[/b]~gi' Amazing resource, thank you very much! Quote Link to comment Share on other sites More sharing options...
xyph Posted June 20, 2012 Share Posted June 20, 2012 Yeah... the manual does kinda rock. Quote Link to comment Share on other sites More sharing options...
RobertP Posted June 20, 2012 Author Share Posted June 20, 2012 so, is it possible to make this expression any simpler? /\[b\](.*?)\[\/b\]/is i tried this, but i want to remove the backward slashes completely if possible. #\[b](.*?)\[/b]#is Quote Link to comment Share on other sites More sharing options...
xyph Posted June 20, 2012 Share Posted June 20, 2012 No, you will always have to escape the [. It's a special character within RegEx. You could make it it's own character class, but that will actually add complexity and slow down the RegEx [[]b](.*?)[[]/b] You could use a non-special character enclosure for your BBCode... {b}(.*?){/b} ... but that would change the behaviour of the application entirely. Quote Link to comment Share on other sites More sharing options...
RobertP Posted June 21, 2012 Author Share Posted June 21, 2012 Thank you very much. i think i am going to update my parser to use this method. much easier on the eyes after a few hours :S #\[b](.*?)\[/b]#is Quote Link to comment 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.