note 128130 added to function.filter-var-array
| From: | Anonymous at main dot php dot net | Date: | Thu, 19 Jan 2023 19:07:06 +0000 |
| Subject: | note 128130 added to function.filter-var-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-220085@lists.php.net to get a copy of this message | ||
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"
}
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.158.213 (proxied: 2604:3d08:5e7b:8850:a1b0:192d:31b4:a8b7)
----
Manual Page -- https://php.net/manual/en/function.filter-var-array.php
Edit -- https://main.php.net/note/edit/128130
Del: integrated -- https://main.php.net/note/delete/128130/integrated
Del: useless -- https://main.php.net/note/delete/128130/useless
Del: bad code -- https://main.php.net/note/delete/128130/bad+code
Del: spam -- https://main.php.net/note/delete/128130/spam
Del: non-english -- https://main.php.net/note/delete/128130/non-english
Del: in docs -- https://main.php.net/note/delete/128130/in+docs
Del: other reasons-- https://main.php.net/note/delete/128130
Reject -- https://main.php.net/note/reject/128130
Search -- https://main.php.net/manage/user-notes.php