Jump to content

Recommended Posts

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);

?>

Link to comment
https://forums.phpfreaks.com/topic/264472-bbcode-issue/
Share on other sites

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!

Link to comment
https://forums.phpfreaks.com/topic/264472-bbcode-issue/#findComment-1355555
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/264472-bbcode-issue/#findComment-1355562
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.