関数 quotemeta

関数 quotemeta は、文字列内の各特殊文字の前にバックスラッシュを追加します。 以下の文字がエスケープされます: . \ + * ? [ ^ ] ( $ )。 この関数は、処理する文字列という1つのパラメータを受け取ります。

構文

phpquotemeta(string);

例

単純な文字列での特殊文字のエスケープ:

php<?php
  echo quotemeta('Hello.world (test)');
?>

コード実行結果:

php'Hello\.world \(test\)'

例

複数の特殊文字を含む文字列のエスケープ:

php<?php
  echo quotemeta('1+1=2? [maybe]');
?>

コード実行結果:

php'1\+1\=2\? \[maybe\]'

例

特殊文字を含まない文字列の処理:

php<?php
  echo quotemeta('simple text');
?>

コード実行結果:

php'simple text'

関連項目

  • 関数 addslashes,
    スラッシュで文字列をエスケープします
  • 関数 preg_quote,
    正規表現の文字をエスケープします