PHP filter_input_array() 函数

PHP 教程 · 2019-01-25 18:28:39

定义和用法

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

查看所有标签

Responsive Web Design

Responsive Web Design

Ethan Marcotte / Happy Cog / 2011-6 / USD 18.00

From mobile browsers to netbooks and tablets, users are visiting your sites from an increasing array of devices and browsers. Are your designs ready? Learn how to think beyond the desktop and craft be......一起来看看 《Responsive Web Design》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具