Home>Article>Backend Development> How to escape regular expressions in PHP

本文主要讲述了如何利用preg_quote()函数用来对正则表达式字符串进行转义
1.语法:
string preg_quote ( string $str [, string $delimiter = NULL ] )
$str: 输入字符串。
$delimiter: 如果指定了可选参数delimiter,它也会被转义。这通常用于 转义PCRE 函数使用的分隔符。/是最通用的分隔符。
preg_quote()需要参数str并向其中 每个正则表达式语法中的字符前增加一个反斜线。 这通常用于你有一些运行时字符串 需要作为正则表达式进行匹配的时候。
目前支持的正则表达式特殊字符有: . \ + * ? [ ^ ] $ ( ) { } = ! | : - #
PS:字符#在7.3.0版本被增加为需要转义的。
2.示例:
The above is the detailed content of How to escape regular expressions in PHP. For more information, please follow other related articles on the PHP Chinese website!