preg_quote
(PHP 4, PHP 5, PHP 7)
preg_quote - 引用正则表达式字符
描述
string preg_quote ( string $str [, string $delimiter = NULL ] )
preg_quote()取str
并使反斜杠在每一个正则表达式语法的字符的前面。如果您需要在某些文本中匹配运行时字符串,并且该字符串可能包含特殊的正则表达式字符,这非常有用。
特殊的正??则表达式字符是:。\ + *?^ $(){} =!<> | : -
请注意,/不是特殊的正则表达式字符。
注意:请注意,preg_quote()并不打算应用于preg_replace()等的$ replacement字符串。
参数
str
输入字符串。
delimiter
如果指定了可选的分隔符,它也将被转义。 这对于转义PCRE功能所需的分隔符非常有用。 /是最常用的分隔符。
返回值
返回带引号(转义)的字符串。
更新日志
版 | 描述 |
---|---|
5.3.0 | 现在引用 - 字符 |
例子
示例#1 preg_quote()示例
<?php
$keywords?=?'$40?for?a?g3/400';
$keywords?=?preg_quote($keywords,?'/');
echo?$keywords;?//?returns?\$40?for?a?g3\/400
?>
示例#2 在某些文本中对单词进行斜体处理
<?php
//?In?this?example,?preg_quote($word)?is?used?to?keep?the
//?asterisks?from?having?special?meaning?to?the?regular
//?expression.
$textbody?=?"This?book?is?*very*?difficult?to?find.";
$word?=?"*very*";
$textbody?=?preg_replace?("/"?.?preg_quote($word,?'/')?.?"/",
??????????????????????????"<i>"?.?$word?.?"</i>",
??????????????????????????$textbody);
?>
注意
注意:此功能是二进制安全的。
扩展内容
- escapeshellcmd() - 转义shell元字符
← preg_match
preg_replace_callback_array →
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com