Page 1 of 1

Apply a common filter with filter_var_array??

Posted: Thu Mar 19, 2009 12:57 pm
by charger
Hi,

I've tried various combinations and permutations of filter_var_array() and have yet to find the answer to this:

Given a rather large associative array, I need to apply FILTER_SANITIZE_STRING with the FILTER_FLAG_ENCODE_HIGH flag to all of the elements in the array.

So:

filter_var_array($my_big_array,FILTER_SANITIZE_STRING);

applies the FILTER_SANITIZE_STRING filter but without the FILTER_FLAG_ENCODE_HIGH flag.

How can I specify FILTER_SANITIZE_STRING + FILTER_FLAG_ENCODE_HIGH with filter_var_array? (easily)

I want to avoid having to create a filter+flag+option array for every element in $my_big_array.

Any Help?

M

Re: Apply a common filter with filter_var_array??

Posted: Thu Mar 19, 2009 1:50 pm
by Christopher
I looked at the manual:

http://us2.php.net/manual/en/function.f ... -array.php

It looks like you do this:

Code: Select all

$args = array(
     'myelement'    => array(
          'filter' => FILTER_SANITIZE_STRING,
          'flags'  => FILTER_FLAG_ENCODE_HIGH,
          ),
     );
filter_var_array($my_big_array, $args);

Re: Apply a common filter with filter_var_array??

Posted: Thu Mar 19, 2009 2:04 pm
by charger
Thanks for the reply,

But this is what I am trying to avoid.

I have 320 elements in the array, I'd like to apply the common filter+flags to all of them without having to define 320 filter+flags arrays that are all the same:

Code: Select all

 
$args = array(
'myelement'    => array(
'filter' => FILTER_SANITIZE_STRING,
'flags'  => FILTER_FLAG_ENCODE_HIGH,
),
'myelement1'    => array(
'filter' => FILTER_SANITIZE_STRING,
'flags'  => FILTER_FLAG_ENCODE_HIGH,
),
'myelement2'    => array(
'filter' => FILTER_SANITIZE_STRING,
'flags'  => FILTER_FLAG_ENCODE_HIGH,
),
'myelement3'    => array(
'filter' => FILTER_SANITIZE_STRING,
'flags'  => FILTER_FLAG_ENCODE_HIGH,
),
'myelement4'    => array(
'filter' => FILTER_SANITIZE_STRING,
'flags'  => FILTER_FLAG_ENCODE_HIGH,
),
....320 times
);
filter_var_array($my_big_array, $args);
 
And I looked at the manual too...You can apply a common filter to all of the elements in one line:

Code: Select all

filter_var_array($my_big_array,FILTER_SANITIZE_STRING);
but there is no mention of how to use flags with that filter.

M