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: Sun, 25 Nov 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 関数が使用する デリミタをエスケープする場合に便利です。'/' がデリミタとしては 最も一般的に使用されています。

返り値

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

Example#1 preg_quote() の例

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

Example#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);
?>

注意

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



add a note add a note User Contributed Notes
preg_quote
Anonymous
27-Dec-2007 08:13
Wondering why your preg_replace fails, even if you have used preg_quote?

Try adding the delimiter / - preg_quote($string, '/');

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