是否有 PHP 函数可以在应用正则表达式模式之前对其进行转义? [英] Is there a PHP function that can escape regex patterns before they are applied?
问题描述
是否有 PHP 函数可以在应用正则表达式模式之前对其进行转义?
我正在寻找类似于 C# Regex.Escape()
函数.
preg_quote()
是您要查找的内容:
说明
string preg_quote ( string $str [, string $delimiter = NULL ] )
preg_quote() 接受 str
并放入一个每个字符前面的反斜杠这是正则表达式的一部分句法.如果您有一个您需要匹配的运行时字符串在某些文本中,字符串可能包含特殊的正则表达式字符.
特殊的正则表达式字符是:<代码>. + * ?[ ^ ] $ ( ) { } = !<>|:-
参数
str
输入字符串.
分隔符
如果指定了可选的分隔符,它也会被转义.这对于转义 PCRE 函数所需的分隔符很有用./是最常用的分隔符.
重要的是,请注意,如果未指定 $delimiter
参数,delimiter - 用于包含正则表达式的字符,通常是正斜杠 (/
) - 不会被转义.您通常希望将与正则表达式一起使用的任何分隔符作为 $delimiter
参数传递.
示例 - 使用 preg_match
查找被空格包围的给定 URL:
$url = 'http://stackoverflow.com/questions?sort=newest';//preg_quote 对 URL 中的点、问号和等号进行转义(通过//默认)以及所有正斜杠(因为我们将/"作为//$delimiter 参数).$escapedUrl = preg_quote($url, '/');//我们在此处将正则表达式括在/"字符中 - 我们传递的分隔符相同//到 preg_quote$regex = '/s' .$escapedUrl .'s/';//$regex 现在是:/shttp://stackoverflow.com/questions?sort=newests/$haystack = "bla bla http://stackoverflow.com/questions?sort=newest bla bla";preg_match($regex, $haystack, $matches);var_dump($matches);//数组(1) {//[0]=>//string(48) " http://stackoverflow.com/questions?sort=newest "//}
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.
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.
Example - using 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=newests/
$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 "
// }
这篇关于是否有 PHP 函数可以在应用正则表达式模式之前对其进行转义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!