Php FILTER_CALLBACK with closing

Trying to skip closing in filter_var_array () but cannot 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 completing the above value, $ fields is an empty array.

Note. Individual filtering works fine:

 $field1= filter_var( $_POST['field1'], FILTER_CALLBACK, array( 'options' => $clean ) ); 

Any ideas?

+4
source share
2 answers

You pass $_POST values ​​without their keys, so there will be no callbacks. Just go to the whole $_POST array, e.g.

 $fields = filter_var_array( $_POST, array( 'field1' => $args, 'field2' => $args, 'field3' => $args ) ); 
+3
source

filter_var_array expects an array with string keys containing the data to filter, and an array that defines the arguments. The correct key is a string containing the name of the variable, and the valid value is either a filter type or an array that optionally indicates the filter, flags, and parameters.

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); 
+1
source

Source: https://habr.com/ru/post/1437846/


All Articles