preg_quote()您正在寻找的是:
描述
string preg_quote ( string $str [, string $delimiter = NULL ] )preg_quote() 取
str
并把反斜杠在每一个正则表达式语法的字符的前面。如果您有需要在某些文本中匹配的运行时字符串,并且该字符串可能包含特殊的正则表达式字符,这将很有用。特殊的正则表达式字符为:
. + * ? [ ^ ] $ ( ) { } = ! < > | : -参量
力量
输入字符串。
定界符
如果指定了可选的定界符,则也将对其进行转义。这对于转义PCRE功能所需的分隔符很有用。/是最常用的定界符。
重要的是,请注意,如果
$delimiter未指定参数,定界符
-用于将您的正则表达式括起来的字符,通常是正斜杠(
/)-将不会转义。通常,您将需要使用正则表达式作为
$delimiter参数传递任何定界符。
示例- 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=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 "// }


