PHP filter_input_array() 函数
定义和用法
filter_input_array() 函数从脚本外部获取多项输入(比如表单输入),并进行过滤。
该函数对过滤多个输入变量很有用,无需重复调用 filter_input()。
该函数可从各种来源获取输入:
- INPUT_GET
- INPUT_POST
- INPUT_COOKIE
- INPUT_ENV
- INPUT_SERVER
- INPUT_SESSION(尚未推行)
- INPUT_REQUEST(尚未推行)
如果成功,则以数组形式返回被过滤的数据。如果失败,则返回 FALSE。
语法
filter_input_array(input_type, filter_args)
| 参数 | 描述 |
|---|---|
| input_type | 必需。规定输入类型。参见上面的列表中可能的类型。 |
| filter_args | 可选。规定过滤器参数数组。合法的数组键名是变量名,合法的值是过滤器 ID,或者规定过滤器、标志以及选项的数组。 该参数也可以是一个单一的过滤器 ID,如果是这样,输入数组中的所有值由指定过滤器进行过滤。 过滤器 ID 可以是 ID 名称(比如 FILTER_VALIDATE_EMAIL)或 ID 号(比如 274)。 |
提示和注释
提示:参见 完整的 PHP Filter 参考手册,查看可与该函数一同使用的过滤器。
实例
在本例中,我们使用 filter_input_array() 函数来过滤三个 POST 变量。所接收的 POST 变量是姓名、年龄以及 e-mail 地址:
<?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 )
点击查看所有 PHP 教程 文章: https://www.codercto.com/courses/l/5.html
The Smashing Book
Jacob Gube、Dmitry Fadeev、Chris Spooner、Darius A Monsef IV、Alessandro Cattaneo、Steven Snell、David Leggett、Andrew Maier、Kayla Knight、Yves Peters、René Schmidt、Smashing Magazine editorial team、Vitaly Friedman、Sven Lennartz / 2009 / $ 29.90 / € 23.90
The Smashing Book is a printed book about best practices in modern Web design. The book shares technical tips and best practices on coding, usability and optimization and explores how to create succes......一起来看看 《The Smashing Book》 这本书的介绍吧!