Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php FILTER_CALLBACK with closure

Tags:

php

filter

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?

like image 791
jbarreiros Avatar asked Oct 04 '12 16:10

jbarreiros


1 Answers

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 
    )
);
like image 169
Gordon Avatar answered Sep 27 '22 23:09

Gordon