百科狗-知识改变命运!
--

filter_input_array() - 获取一系列外部变量,并且可以通过过滤器处理它们 - php 过滤函数

梵高12个月前 (11-21)阅读数 46#技术干货
文章标签是一个

filter_input_array()

(PHP 5 >= 5.2.0, PHP 7)

获取一系列外部变量,并且可以通过过滤器处理它们

说明

filter_input_array(int $type[,mixed $definition[,bool $add_empty= true]]): mixed

这个函数当需要获取很多变量却不想重复调用filter_input()时很有用。

参数

$type

INPUT_GET,INPUT_POST,INPUT_COOKIE,INPUT_SERVER,orINPUT_ENV之一。

$definition

一个定义参数的数组。一个有效的键必须是一个包含变量名的string,一个有效的值要么是一个filter type,或者是一个array指明了过滤器、标示和选项。如果值是一个数组,那么它的有效的键可以是filter,用于指明filter type,flags用于指明任何想要用于过滤器的标示,或者options用于指明任何想要用于过滤器的选项。参考下面的例子来更好的理解这段说明。

这个参数也可以是一个filter constant的整数。那么数组中的所有变量都会被这个过滤器所过滤。

$add_empty

在返回值中添加NULL作为不存在的键。

返回值

如果成功的话返回一个所请求的变量的数组,如果失败的话返回FALSE。对于数组的值,如果过滤失败则返回FALSE,如果$variable_name不存在的话则返回NULL。如果标示FILTER_NULL_ON_FAILURE被使用了,那么当变量不存在时返回FALSE,当过滤失败时返回NULL

范例

一个filter_input_array()的例子

filter_input_array() - 获取一系列外部变量,并且可以通过过滤器处理它们 - php 过滤函数

以上例程会输出:

array(6) {
  ["product_id"]=>
  string(17) "libgd%3Cscript%3E"
  ["component"]=>
  array(1) {
    [0]=>
    int(10)
  }
  ["versions"]=>
  string(6) "2.0.33"
  ["doesnotexist"]=>
  NULL
  ["testscalar"]=>
  bool(false)
  ["testarray"]=>
  array(1) {
    [0]=>
    int(2)
  }
}

更新日志

版本说明
5.4.0添加$add_empty参数.

注释

Note:

INPUT_SERVER数组中并没有REQUEST_TIME,因为它是被稍后插入到$_SERVER中的。

参见

  • filter_input() 通过名称获取特定的外部变量,并且可以通过过滤器处理它
  • filter_var_array() 获取多个变量并且过滤它们
  • Types of filters
Note that although you can provide a default filter for the entire input array there is no way to provide a flag for that filter without building the entire definition array yourself.
So here is a small function that can alleviate this hassle! 
[New Version]
This function is very useful for filtering complicated array structure.
Also, Some integer bitmasks and invalid UTF-8 sequence detection are available.
Code: 
[New Version]
Example Usage:

Example Result:
array(3) {
 ["A"]=>
 array(4) {
  ["a"]=>
  string(36) " CORRECT(including some spaces)  "
  ["b"]=>
  string(30) "CORRECT(including some spaces)"
  ["c"]=>
  string(0) ""
  ["d"]=>
  string(0) ""
 }
 ["B"]=>
 array(3) {
  ["a"]=>
  string(36) " CORRECT(including some spaces)  "
  ["b"]=>
  string(0) ""
  ["c"]=>
  string(0) ""
 }
 ["C"]=>
 array(3) {
  ["a"]=>
  string(30) "CORRECT(including some spaces)"
  ["b"]=>
  string(0) ""
  ["c"]=>
  string(0) ""
 }
}
This function is very useful for filtering complicated array structure.
Code:

Sample Usage:

Sample Result:
array(2) {
 ["a"]=>
 string(21) "DEFAULT"
 ["b"]=>
 array(2) {
  ["c"]=>
  string(12) "CORRECT"
  ["d"]=>
  string(21) "DEFAULT"
 }
}
Beware: if none of the arguments is set, this function returns NULL, not an array of NULL values.
/* No POST vars set in request
$_POST = array();
*/
$args = array('some_post_var' => FILTER_VALIDATE_INT);
$myinputs = filter_input_array(INPUT_POST, $args);
var_dump($myinputs);
Expected Output: array(1) { ["some_post_var"]=> NULL } 
Actual Output: NULL
If you are trying to handling multiple form inputs with same name, then you must assign the `'flags' => FILTER_REQUIRE_ARRAY` to the definitions entry.
Example, you have a html form as such:

 
 

Your definitions array will look a little like this:
$args = array(
 't1'  => array(
   'name' => 't1',
   'filter' => FILTER_SANITIZE_STRING,
   'flags' => FILTER_REQUIRE_ARRAY)
);
Looks like filter_input_array isn't aware of changes to the input arrays that were made before calling filter_input_array. Instead, it always looks at the originally submitted input arrays.
So this will not work:
$_POST['my_float_field'] = str_replace(',','.',$_POST['my_float_field']);
$args = array('my_float_field',FILTER_VALIDATE_FLOAT);
$result = filter_input_array(INPUT_POST, $args);
While filtering input arrays, be careful of what flags you set besides FILTER_REQUIRE_ARRAY. For example, setting the flags like so:

.. will result in a blank $form_inputs['myInputArr'] regardless of what $_POST['myInputArr'] contains.
The above example raises other questions such as how one would validate an html array. In the input form each input tag that refers to an html array would be named for example testarray[]. However, after the form is submitted, the syntax for validating the values is different from the expected $_POST['testarray[]']. Instead one has to drop the braces and validate as follows, assuming that testarray[] is supposed to be an html array of numerical values:
Valid test:
echo '*';
echo filter_input(
INPUT_POST,
'testarray',
FILTER_VALIDATE_INT,
FILTER_REQUIRE_ARRAY
);
echo '*';
But the following is an invalid test that results in 2 consequtive asterisks only!
echo '*';
echo filter_input(INPUT_POST,
'testarray[]',
FILTER_VALIDATE_INT,
FILTER_REQUIRE_ARRAY
);
echo '*';
So, there is a naming inconsistency going on, as after the form is submitted, one has to forget about the original name of the submitted array by dropping its braces. Maybe when the PECL/Filter extension is reviewed again, the great ones might consider making the syntax a little more forgiving.
@iam4webwork
This is not specific to filter_input. If you have an element in HTML called names[], it can be accessed by calling $_POST['names'].
The above example will actually output "NULL" because of the undefined variable doesnotexist - see http://bugs.php.net/bug.php?id=42608.
extract() is a very convenient way of copying all those variables to the local scope. (see http://www.php.net/extract)

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)