是否有 PHP 函数可以在应用正则表达式模式之前对其进行转义?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1531456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:58:07  来源:igfitidea点击:

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

phpregexescaping

提问by vfclists

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

是否有 PHP 函数可以在应用正则表达式模式之前对其进行转义?

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

我正在寻找与 C#Regex.Escape()函数类似的东西。

回答by Tom Haigh

preg_quote()is what you are looking for:

preg_quote()是你要找的:

Description

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

preg_quote()takes strand 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.

描述

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

preg_quote()str在作为正则表达式语法一部分的每个字符前面取并放置一个反斜杠。如果您有一个需要在某些文本中匹配的运行时字符串并且该字符串可能包含特殊的正则表达式字符,这将非常有用。

特殊的正​​则表达式字符是: . \ + * ? [ ^ ] $ ( ) { } = ! < > | : -

参数

字符串

输入字符串。

分隔符

如果指定了可选的分隔符,它也会被转义。这对于转义 PCRE 函数所需的分隔符很有用。/ 是最常用的分隔符。

Importantly, note that if the $delimiterargument 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 $delimiterargument.

重要的是,请注意,如果$delimiter未指定参数,分隔符- 用于包含正则表达式的字符,通常是正斜杠 ( /) - 将不会被转义。您通常希望将与正则表达式一起使用的任何分隔符作为$delimiter参数传递。

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

示例 -preg_match用于查找被空格包围的给定 URL 的出现:

$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 "
// }

回答by Danon

It would be much safer to use Prepared Patternsfrom T-Regx library:

使用来自T-Regx 库的Prepared Patterns会更安全:

$url = 'http://stackoverflow.com/questions?sort=newest';

$pattern = Pattern::prepare(['\s', [$url], '\s']);
                                // ↑ $url is quoted

then perform normal t-regxmatch:

然后执行正常的t-regx匹配:

$haystack = "Bla bla http://stackoverflow.com/questions?sort=newest bla bla";

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