PHP preg_quote()関数
例
preg_quote()を使用して、正規表現で特殊文字を安全に使用します。
<?php
$search = preg_quote("://", "/");
$input = 'https://www.w3schools.com/';
$pattern = "/$search/";
if(preg_match($pattern, $input)) {
echo
"The input is a URL.";
} else {
echo "The input is not a URL.";
}
?>
定義と使用法
このpreg_quote()
関数は、正規表現で特別な意味を持つ文字に円記号を追加して、リテラル文字の検索を実行できるようにします。この関数は、正規表現でユーザー入力を使用する場合に役立ちます。
構文
preg_quote(input, delimiter)
パラメータ値
Parameter | Description |
---|---|
input | Required. The string to be escaped |
delimiter | Optional. Defaults to null. This parameter expects a single character indicating which delimiter the regular expression will use. When provided, instances of this character in the input string will also be escaped with a backslash |
技術的な詳細
戻り値: | バックスラッシュでエスケープされたすべての特殊文字を含む文字列を返します |
---|---|
PHPバージョン: | 4歳以上 |
変更ログ: | PHP 7.3-#文字は特殊文字と見なされ、エスケープされます。 PHP 5.3 ---文字は特殊文字と見なされ、エスケープされます。 |
❮PHPRegExpリファレンス