PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<preg_matchpreg_replace_callback>
Last updated: Mon, 16 Jul 2012

preg_quote

(PHP 3 >= 3.0.9, PHP 4, PHP 5)

preg_quote -- 转义正则表达式字符

说明

string preg_quote ( string str [, string delimiter] )

preg_quote()str 为参数并给其中每个属于正则表达式语法的字符前面加上一个反斜线。如果你需要以动态生成的字符串作为模式去匹配则可以用此函数转义其中可能包含的特殊字符。

如果提供了可选参数 delimiter,该字符也将被转义。可以用来转义 PCRE 函数所需要的定界符,最常用的定界符是斜线 /。

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

例子 1. preg_quote() 例子

<?php
$keywords
= "$40 for a g3/400";
$keywords = preg_quote ($keywords, "/");
echo
$keywords; // returns \$40 for a g3\/400
?>

例子 2. 给某文本中的一个单词加上斜体标记

<?php
// 本例中,preg_quote($word) 用来使星号不在正则表达式中
// 具有特殊含义。

$textbody = "This book is *very* difficult to find.";
$word = "*very*";
$textbody = preg_replace ("/".preg_quote($word)."/",
                        
"<i>".$word."</i>",
                        
$textbody);
?>




<preg_matchpreg_replace_callback>
 Last updated: Mon, 16 Jul 2012
 
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: http://manual.phpv.net/
Last updated: Thu Jul 7 19:13:47 2005 CST