Reputation: 998
I have looked at the documentation that PHP provides on PCRE Patterns. I am using a third party plugin to handle some text from the user, and the following preg_replace is failing because of missing terminating ] char. (preg_replace(): Compilation failed: missing terminating ] for character class
$input = preg_replace('/[\]/i','',$userInput);
From what I can see the terminating delimiter is / with a character class that only has a \ in it. The i, if I can read correctly tells the expression to not care about upper or lower case. I see both opening [ and closing ].
Why is it throwing the error? What is the preg_replace trying to do?
Upvotes: 1
Views: 76
Reputation: 12806
You need to escape the \
otherwise it escapes the ]
(and you need to escape it twice, once for the PHP string and once for PCRE).
$input = preg_replace('/[\\\]/i','',$userInput);
And you can omit the [
and ]
altogether (as well as the i
).
$input = preg_replace('/\\\/','',$userInput);
Or, you can just use str_replace
:
$input = str_replace('\\','',$userInput);
Upvotes: 3