PHP quotemeta() 関数

定義された文字の前に反斜線を追加します:

<?php
$str = "Hello world. (can you hear me?)";
echo quotemeta($str);
?>

実行例

定義と使用方法

quotemeta() 関数は文字列中のプレデファインドの文字前にアンカーを追加します。

プレデファインドの文字:

  • ピリオド(.)
  • アンカー(\)
  • プラス記号(+)
  • アスパース記号(*)
  • クエスチョンマーク(?)
  • 角括弧([])
  • キャロル記号(^)
  • ドル記号($)
  • 丸括弧(())

ヒント:この関数は特殊な意味を持つ文字(例えば SQL の ( )、[ ] または * )をエスケープするために使用できます。

注釈:この関数はバイナリセキュアです。

文法

quotemeta(string)
パラメータ 説明
string 必須。チェックする文字列を指定します。

技術的詳細

返り値: 参照元文字の文字列を返します。
PHP バージョン: 4+

さらに多くの例

例 1

複数のプレデファインド文字前にアンカーを追加します:

<?php
$str1 = "1 + 1 = 2";
$str2 = "1 * 1 = 1";
$str3 = "Could you borrow me 5$?";
$str4 = "Are you not e&";
$str5 = "The caret [ ^ ] Looks like a hat!";
echo quotemeta($str1)."<br>";
echo quotemeta($str2)."<br>";
echo quotemeta($str3)."<br>";
echo quotemeta($str4)."<br>";
echo quotemeta($str5)."<br>";
?>

実行例