栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/435287.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号