PHP filter_input_array()Function


PHP Filter 参考手册Complete PHP Filter Reference Manual

Definition and Usage

The filter_input_array() function gets multiple inputs from outside the script (such as form input), and filters them.

This function is useful for filtering multiple input variables without repeatedly calling filter_input().

This function can get input from various sources:

  • INPUT_GET
  • INPUT_POST
  • INPUT_COOKIE
  • INPUT_ENV
  • INPUT_SERVER
  • INPUT_SESSION (not yet implemented)
  • INPUT_REQUEST (not yet implemented)

If successful, returns the filtered data as an array. If it fails, returns FALSE.

Syntax

filter_input_array(input_type, filter_args)

Parameter Description
input_type Required. Specifies the input type. See the list above for possible types.
filter_args Optional. Specifies the filter parameter array. Valid array key names are variable names, valid values are filter IDs, or an array specifying filters, flags, and options.

This parameter can also be a single filter ID. If so, all values in the input array are filtered by the specified filter.

Filter IDs can be ID names (such as FILTER_VALIDATE_EMAIL) or ID numbers (such as 274).



Tips and Notes

Tip:See theComplete PHP Filter Reference Manualto view filters that can be used with this function.


Examples

In this example, we use the filter_input_array() function to filter three POST variables. The received POST variables are name, age, and e-mail address:

<?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));
?>

The output of the code is shown below:

Array
(
[name] => Peter
[age] => 41
[email] => [email protected]
)


PHP Filter 参考手册Complete PHP Filter Reference Manual Other Extensions