2

The following function

function stripbbtags ($string) {
    $pattern = "/\[([^\]]+?)(=[^\]]+?)?\](.+?)\[/\1\]/";
    $replace = "";
    return preg_replace($pattern, $replace, $string);
}

returns an error:

Unknown modifier

with some cryptic character that looks like SOH in Notepad++ but has a black background. Really weird.

1
  • Do you wish to remove just the tags (and keep their content)? And does this flavor of BBCode allow nested tags? (e.g. Many BBCode parsers allow [quote] tags to be nested.) If so, your solution will need to be a bit more complex. The one you have now will not work correctly with nested tags. Commented Dec 21, 2011 at 17:06

2 Answers 2

2

How about this:

function stripbbtags ($string) {
    $pattern = "#\[([^\]]+?)(=[^\]]+?)?\](.+?)\[/\1\]#";
    $replace = "";
    return preg_replace($pattern, $replace, $string);
}

The error message is a bit cryptic but actually give you some insight, if take a look at the documentation of preg_replace, you would notice they talk about modifiers. Those modifiers are used to pass options to the PCRE library, i.e. do some case insensitive match, string is in unicode etc.

The problem relies in the character you are using as separator; you are using / and the regular expression contain a slash so PCRE thinks \1\]/ is your modifier. Changing the separator to # fixes the problem.

Sign up to request clarification or add additional context in comments.

Comments

0

Well, your delimiter is / and after your closing delimiter is the string \1\]/, but neither \1 nor the other characters are valid modifiers.

Pick a different delimiter, or simply escape all occurrences of it within your expression.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.