Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: preg_quote - Manual
[go: Go Back, main page]

PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

preg_replace_callback" width="11" height="7"/> <preg_match
Last updated: Thu, 31 May 2007

view this page in

preg_quote

(PHP 4, PHP 5)

preg_quote — 正規表現文字をクオートする

説明

string preg_quote ( string $str [, string $delimiter] )

preg_quote() は、str を引数とし、正規表現構文の特殊文字の前にバックスラッシュを挿入します。 この関数は、実行時に生成される文字列をパターンとしてマッチングを行う必要があり、 その文字列には正規表現の特殊文字が含まれているかも知れない場合に有用です。

正規表現の特殊文字は、次のものです。 . \ + * ? [ ^ ] $ ( ) { } = ! < > | :

パラメータ

str

入力文字列。

delimiter

オプションの delimiter を指定すると、 ここで指定した文字もエスケープされます。これは、PCRE 関数が使用する デリミタをエスケープする場合に便利です。'/' がデリミタとしては 最も一般的に使用されています。

返り値

クォートされた文字列を返します。

例 1674. preg_quote() の例

<?php
$keywords
= '$40 for a g3/400';
$keywords = preg_quote($keywords, '/');
echo
$keywords; // \$40 for a g3\/400 を返します
?>

例 1675. テキスト内の単語の斜体変換

<?php
// この例では、preg_quote($word) を使って、アスタリスクが
// 正規表現での特殊な意味を帯びないようにしています

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

注意

注意: この関数はバイナリデータに対応しています。



add a note add a note User Contributed Notes
preg_quote
chad
05-Dec-2006 07:03
If you are placing your quoted string within []'s, make sure you also escape the dash (-) character manually.

preg_match("/^[A-Za-z\d" . preg_quote('!@#$%^&*()-_=+\|[]{};:/?.><', '/') . "]{8,32}$/", $password)

The above will try to match the characters ')' through '_' whatever those are. So use the below expression:

preg_match("/^[A-Za-z\d" . preg_quote('!@#$%^&*()_=+\|[]{};:/?.><', '/') . "\-]{8,32}$/", $password)

Yes, I know I can use \w in place of A-Za-z, but I wanted to illustrate my point better :)
mina86 at tlen dot pl
26-Dec-2003 08:02
Re: adrian holovaty
You must also escape '#' character. Next thing is that there is more then one whitespace character (a space).. Also IMO the name preg_quote_white() won't tell what the new function does so we could rename it. And finally, we should also add $delimiter:

<?php
function preg_xquote($a, $delimiter = null) {
     if (
$delimiter) {
          return
preg_replace('/[\s#]/', '\\\0', preg_quote($a, substr("$delimiter", 0, 1)));
     } else {
          return
preg_replace('/[\s#]/', '\\\0', preg_quote($a));
     }
}
?>
adrian holovaty
16-Jul-2003 09:12
Note that if you've used the "x" pattern modifier in your regex, you'll want to make sure you escape any whitespace in your string that you *want* the pattern to match.

A simplistic example:

$phrase = 'a test'; // note the space
$textbody = 'this is a test';

// Does not match:
preg_match('/' . preg_quote($phrase) . '$/x', $textbody);

function preg_quote_white($a) {
     $a = preg_quote($a);
     $a = str_replace(' ', '\ ', $a);
     return $a;
}

// Does match:
preg_match('/' . preg_quote_white($phrase) . '$/x', $textbody);

 
show source | credits | sitemap | contact | advertising | mirror sites