preg_quoteで特殊文字をエスケープ

preg_quoteを使うと、正規表現で特殊な文字として扱われる

. \ + * ? [ ^ ] $ ( ) { } = ! < > | :

を、エスケープすることができます。

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

これは便利ですね。次正規表現を使う際には是非活用してみようと思います。メモメモ。

参考URL

関連する記事:

Powered by

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">