PHP filter_input_array()関数
例
filter_input_array()関数を使用して、3つのPOST変数をフィルタリングします。受信したPOST変数は、名前、年齢、および電子メールです。
<?php
$filters = array (
"name" => array ("filter"=>FILTER_CALLBACK,
"flags"=>FILTER_FORCE_ARRAY,
"options"=>"ucwords"
),
"age" => array ( "filter"=>FILTER_VALIDATE_INT,
"options"=>array("min_range"=>1,"max_range"=>120)
),
"email" => FILTER_VALIDATE_EMAIL
);
print_r(filter_input_array(INPUT_POST, $filters));
?>
上記のコードの出力は次のようになります。
定義と使用法
filter_input_array()関数は、外部変数(たとえば、フォーム入力から)を取得し、オプションでそれらをフィルター処理します。
この関数は、filter_input()を何度も呼び出す代わりに、多くの値を取得/フィルタリングする場合に役立ちます。
構文
filter_input_array(type, definition, add_empty)
パラメータ値
Parameter | Description |
---|---|
type | Required. The input type to check for. Can be one of the following:
|
definition | Optional. Specifies an array of filter arguments. A valid array key is a variable name, and a valid value is a filter name or ID, or an array specifying the filter, flags and options. This parameter can also be a single filter name/ID; then all values in the input array are filtered by the specified filter |
add_empty | Optional. A Boolean value. TRUE adds missing keys as NULL to the return value. Default value is TRUE |
技術的な詳細
戻り値: | 成功した場合は変数の値、失敗した場合はFALSEの配列 |
---|---|
PHPバージョン: | 5.2+ |
PHP変更ログ: | PHP5.4 - add_emptyパラメーターが追加されました |
❮完全なPHPフィルターリファレンス