★ wanayoo — archive 1999 http://www.php.net/manual/function.preg-quote.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to PCRE
Quick Reference
German version of this pageJapanese version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
PCRE
* preg_match
* preg_match_all
* preg_replace
* preg_split
* preg_quote
* preg_grep
* Pattern Modifiers
* Pattern Syntax
Manual: preg_quote
View the source code for this pageSearch the site



Previous page
 preg_split
 Updated
Sat, 12 Aug 2000
preg_grep 
Next page


preg_quote

(PHP3 >= 3.0.9, PHP4 )

preg_quote -- Quote regular expression characters

Description

string preg_quote (string str [, string delimiter])

preg_quote() takes str and puts a backslash in front of every character that is part of the regular expression syntax. This is useful if you have a run-time string that you need to match in some text and the string may contain special regex characters.

If the optional delimiter is specified, it will also be escaped. This is useful for escaping the delimeter that is required by the PCRE functions. The / is the most commonly used delimiter.

The special regular expression characters are:
. \\ + * ? [ ^ ] $ ( ) { } = ! < > | :

Example 1.


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

Example 2. Italicizing a word within some text


// 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);
      


User Contributed Notes: preg_quote


monte@ispi.net
18-Nov-1999 05:31
A good example of this is if you want to use a variable as search content that may contain characters with special meaning to PCRE.

Here I am making every occurance of $word italicized in $textbody. The variable $word may contain special characters and they will not affect the regular expression:

$textbody = preg_replace("|\b".preg_quote($word)."\b|","<i>".$word."</i>",$textbody);



 About Notes


Previous page
 preg_split
 Updated
Sat, 12 Aug 2000
preg_grep 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.