文字

filter_var_array

(PHP 5 >= 5.2.0, PHP 7)

filter_var_array获取多个变量并且过滤它们

说明

mixed filter_var_array ( array $data [, mixed $definition [, bool $add_empty = true ]] )

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

参数

data

一个键为字符串,值为待过滤的数据的数组。

definition

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

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

add_empty

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

返回值

如果成功则返回一个包含所请求变量的数组,或者当失败时返回 FALSE 。 一个数组的值如果过滤失败则为 FALSE ,或者为 NULL 如果变量不存在的话。

范例

Example #1 一个 filter_var_array() 的例子

<?php
error_reporting
( E_ALL  E_STRICT );
$data  = array(
    
'product_id'     =>  'libgd<script>' ,
    
'component'      =>  '10' ,
    
'versions'       =>  '2.0.33' ,
    
'testscalar'     => array( '2' '23' '10' '12' ),
    
'testarray'      =>  '2' ,
);

$args  = array(
    
'product_id'    =>  FILTER_SANITIZE_ENCODED ,
    
'component'     => array( 'filter'     =>  FILTER_VALIDATE_INT ,
                            
'flags'      =>  FILTER_FORCE_ARRAY
                            
'options'    => array( 'min_range'  =>  1 'max_range'  =>  10 )
                           ),
    
'versions'      =>  FILTER_SANITIZE_ENCODED ,
    
'doesnotexist'  =>  FILTER_VALIDATE_INT ,
    
'testscalar'    => array(
                            
'filter'  =>  FILTER_VALIDATE_INT ,
                            
'flags'   =>  FILTER_REQUIRE_SCALAR ,
                           ),
    
'testarray'     => array(
                            
'filter'  =>  FILTER_VALIDATE_INT ,
                            
'flags'   =>  FILTER_FORCE_ARRAY ,
                           )

);

$myinputs  filter_var_array ( $data $args );

var_dump ( $myinputs );
echo 
"\n" ;
?>

以上例程会输出:

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

更新日志

版本 说明
5.4.0 添加 add_empty 参数.

参见

  • filter_input_array() - 获取一系列外部变量,并且可以通过过滤器处理它们
  • filter_var() - 使用特定的过滤器过滤一个变量
  • filter_input() - 通过名称获取特定的外部变量,并且可以通过过滤器处理它
  • Types of filters

用户评论:

[#1] xavier [2010-08-18 13:37:07]

When I run the script on my linux box (php 5.2.10) the output of "Example #1 A filter_var_array() example"
is actually:

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)
  }
}

Notice that the values of "product_id" and "versions" are not arrays.  If you add the FILTER_FORCE_ARRAY flag to the "product_id" and "versions" filter arrays then the output returns as it is outlined in the example.

[#2] eguvenc at gmail dot com [2009-06-06 15:45:35]

<?php
//an example of simply sanitize an array..

$data = array(
                
'<b>bold</b>',
                
'<script>javascript</script>',
                
'P*}i@893746%%%p*.i.*}}|.dw<?php echo "echo works!!";?>');

$myinputs filter_var_array($data,FILTER_SANITIZE_STRING);

var_dump($myinputs);

//OUTPUT:
//formarray(3) { [0]=> string(4) "bold" [1]=> string(10) "javascript" [2]=> string(26) "P*}i@893746%%%p*.i.*}}|.dw" }
?>

[#3] Veysel zer [2007-06-08 03:41:31]

Numeric keys are not allowed in the definition array

上一篇: 下一篇: