使用filter_input_array()函数过滤三个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));
?>
上面代码的输出将是:
Array
(
[name] => Peter
[age] => 41
[email] => peter@example.com
)
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 变更日志: | PHP 5.4 - 的添加空添加了参数 |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!