The \ is known as the escape code, which restore the original literal meaning of the following character. Similarly, * , + , ? (occurrence indicators), ^ , $ (position anchors) have special meaning in regex. You need to use an escape code to match with these characters.
An escaping backslash can be used to include a whitespace or "#" character as part of the pattern. A second use of backslash provides a way of encoding non-printing characters in patterns in a visible manner.
In PHP, an escape sequence starts with a backslash \ . Escape sequences apply to double-quoted strings. A single-quoted string only uses the escape sequences for a single quote or a backslash.
preg_quote()
is what you are looking for:
Description
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:
. \ + * ? [ ^ ] $ ( ) { } = ! < > | : -
Parameters
str
The input string.
delimiter
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.
preg_match
to find occurrences of a given URL surrounded by whitespace:$url = 'http://stackoverflow.com/questions?sort=newest';
// 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 http://stackoverflow.com/questions?sort=newest bla bla";
preg_match($regex, $haystack, $matches);
var_dump($matches);
// array(1) {
// [0]=>
// string(48) " http://stackoverflow.com/questions?sort=newest "
// }
It would be much safer to use Prepared Patterns from T-Regx library:
$url = 'http://stackoverflow.com/questions?sort=newest';
$pattern = Pattern::prepare(['\s', [$url], '\s']);
// ↑ $url is quoted
then perform normal match:
$haystack = "Bla bla http://stackoverflow.com/questions?sort=newest bla bla";
$matches = $pattern->match($haystack)->all();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With