From http://php.net/manual/en/function.preg-quote.php:
preg_quote() takes str and puts a backslash in front of every character that is part of the regular expression syntax. This is useful if you have a run-time string that you need to match in some text and the string may contain special regex characters.
The special regular expression characters are:
. \ + * ? [ ^ ] $ ( ) { } = ! < > | : -
Note that
/
is not a special regular expression character.
}
is unnecessary but I can understand why they'd include it for symmetry. E.g. the following code works:
$re = '/}{This is fine}{/';
preg_match($re, $re, $match);
var_dump($match);
The output is:
array(1) {
[0] =>
string(16) "}{This is fine}{"
}
Why do they include = ! < > :
? As far as I can tell, they're only ever special after being introduced by another unescaped meta character, e.g. immediately after (?
, both of which characters also get escaped. :
can also be special inside character classes like so: [[:alpha:]]
, but all four brackets get escaped.