Reputation: 1103
Trying to pass a closure into filter_var_array(), but can't seem to make it work.
$clean = function( $html ) {
return HTML::sanitize( $html, array('p','ul','ol','li'), array('class','style') );
};
$args = array( 'filter' => FILTER_CALLBACK, 'options' => $clean );
$fields = filter_var_array(
array( $_POST['field1'], $_POST['field2'], $_POST['field3'] ),
array( 'field1' => $args, 'field2' => $args, 'field3' => $args )
);
After the above is run, $fields is an empty array.
Note, individual filtering works fine:
$field1= filter_var( $_POST['field1'], FILTER_CALLBACK, array( 'options' => $clean ) );
Any ideas?
Upvotes: 6
Views: 1634
Reputation: 95121
filter_var_array
expects An array with string keys containing the data to filter and An array defining the arguments. A valid key is a string containing a variable name and a valid value is either a filter type, or an array optionally specifying the filter, flags and options.
Your implementation should be like this :
$clean = function ($html) {
return HTML::sanitize($html, array('p','ul','ol','li'), array('class','style'));
};
$filter = array('filter' => FILTER_CALLBACK,'options' => $clean);
$args = array("field1" => $filter,"field2" => $filter,"field3" => $filter);
$fields = filter_var_array($_POST, $args);
Upvotes: 2
Reputation: 316989
You are passing in the values of $_POST
without their keys, hence no callbacks will be triggered. Just pass in the entire $_POST
array instead, e.g.
$fields = filter_var_array(
$_POST,
array(
'field1' => $args,
'field2' => $args,
'field3' => $args
)
);
Upvotes: 4