Is there a PHP function that can escape regex patterns before they are applied?

I am looking for something along the lines of the C# Regex.Escape() function.

Solution 1

preg_quote() is what you are looking for:


string preg_quote ( string $str [, string $delimiter = NULL ] )

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: . \ + * ? [ ^ ] $ ( ) { } = ! < > | : -



The input string.


If the optional delimiter is specified, it will also be escaped. This is useful for escaping the delimiter that is required by the PCRE functions. The / is the most commonly used delimiter.

Importantly, note that if the $delimiter argument is not specified, the delimiter - the character used to enclose your regex, commonly a forward slash (/) - will not be escaped. You will usually want to pass whatever delimiter you are using with your regex as the $delimiter argument.

Example - using preg_match to find occurrences of a given URL surrounded by whitespace:

$url = '';

// preg_quote escapes the dot, question mark and equals sign in the URL (by
// default) as well as all the forward slashes (because we pass '/' as the
// $delimiter argument).
$escapedUrl = preg_quote($url, '/');

// We enclose our regex in '/' characters here - the same delimiter we passed
// to preg_quote
$regex = '/\s' . $escapedUrl . '\s/';
// $regex is now:  /\shttp\:\/\/stackoverflow\.com\/questions\?sort\=newest\s/

$haystack = "Bla bla bla bla";
preg_match($regex, $haystack, $matches);

// array(1) {
//   [0]=>
//   string(48) " "
// }

Solution 2

It would be much safer to use Prepared Patterns from T-Regx:

$url = '';

$pattern = Pattern::inject('\[email protected]\s', [$url]);
                                    //  $url is quoted

then perform normal match:

$haystack = "Bla bla bla bla";

$matcher = pattern->match($haystack);
$matches = $match->all();

you can even use it with preg_match():

preg_match($pattern, 'foo', $matches);