filter_var_array

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

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

说明

filter_var_array(array $array, array|int $options = FILTER_DEFAULT, bool $add_empty = true): array|false|null

不需要重复调用 filter_var() 就能获取多个变量。

参数

array

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

options

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

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

add_empty

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

返回值

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

示例

示例 #1 一个 filter_var_array() 的例子

<?php

$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"]=>
  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 notes

up
4
Anonymous
1 year ago
To apply the same filter to many params/keys, use array_fill_keys().

<?php
$data
= array(
'product_id' => 'libgd<script>',
'component' => ' 10 ',
'versions' => '2.0.33',
'testscalar' => array('2', '23', '10', '12'),
'testarray' => '2',
);
$keys = array(
'product_id',
'component',
'versions',
'doesnotexist',
'testscalar',
'testarray'
);
$options = array(
'filter' => FILTER_CALLBACK,
'options' => function ($value) {
return
trim(strip_tags($value));
},
);
$args = array_fill_keys($keys, $options);
/* Result
$args = array(
'product_id' => array(
'filter' => FILTER_CALLBACK,
'options' => function ($value) {
return trim(strip_tags($value));
},
),
'component' => array(
'filter' => FILTER_CALLBACK,
'options' => function ($value) {
return trim(strip_tags($value));
},
),
'versions' => array(
'filter' => FILTER_CALLBACK,
'options' => function ($value) {
return trim(strip_tags($value));
},
),
'doesnotexist' => array(
'filter' => FILTER_CALLBACK,
'options' => function ($value) {
return trim(strip_tags($value));
},
),
'testscalar' => array(
'filter' => FILTER_CALLBACK,
'options' => function ($value) {
return trim(strip_tags($value));
},
),
'testarray' => array(
'filter' => FILTER_CALLBACK,
'options' => function ($value) {
return trim(strip_tags($value));
},
),
);
*/

$myinputs = filter_var_array($data, $args);
var_dump($myinputs);

Output:

array(
6) {
'product_id' =>
string(5) "libgd"
'component'
=>
string(2) "10"
'versions'
=>
string(6) "2.0.33"
'doesnotexist'
=>
NULL
'testscalar' =>
array(
4) {
[
0] =>
string(1) "2"
[1] =>
string(2) "23"
[2] =>
string(2) "10"
[3] =>
string(2) "12"
}
'testarray' =>
string(1) "2"
}
up
8
eguvenc at gmail dot com
15 years ago
<?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" }
?>
up
1
Vee W.
5 years ago
$emails = [
'a' => 'email1@domain.com',
'b' => '<email2>@domain.com',
];

$result = filter_var_array($emails, FILTER_SANITIZE_EMAIL);
print_r($result);

// the result will be...
// array('a' => 'email1@domain.com', 'b' => 'email2@domain.com')
up
-1
tongcheong77 at gmail dot com
7 years ago
for those who would like to know how to use regular expressions with filter var array. taken from w3 schools.

$data = array(

'regxp' => 'michael'

);

$args = array(

'regxp' => array(

'filter'=>FILTER_VALIDATE_REGEXP | FILTER_SANITIZE_STRING,
'options'=>array('regexp'=>"/^michael$/"),

));

$output=filter_var_array($data,$args);

var_dump($output); // array(1) { ["regxp"]=> string(7) "michael" }

- bruce tong
up
-2
tongcheong77 at gmail dot com
7 years ago
an update to previous post.

using regular expressions along with another filter in filter var array

$data = array(

'regxp' => "`\tcafé\n`"

);

$args = array(

'regxp' => array(

'filter'=>FILTER_VALIDATE_REGEXP | FILTER_SANITIZE_STRING,
'flags'=>FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_BACKTICK,
'options'=>array('regexp'=>"/t(.*)/"),

));

$output=filter_var_array($data,$args);

var_dump($output); // array(1) { ["regxp"]=> string(3) "caf" }

-bruce tong
To Top