downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

filter_var> <filter_list
[edit] Last updated: Fri, 25 May 2012

view this page in

filter_var_array

(PHP 5 >= 5.2.0)

filter_var_arrayRetorna múltiple variables y opcionalmente las filtra

Descripción

mixed filter_var_array ( array $data [, mixed $definition ] )

Esta función es útil para recuperar muchos valores sin llamar repetidamente a filter_var().

Parámetros

data

Un array asociativo de claves en formato strings que contiene los datos a filtrar.

definition

Un array definiendo los argumentos. Una clave válida será aquella que contiene un string con el nombre de una variable y un valor válido aquel que o bien es un tipo filter type o un array especificando opcionalmente el filtro, flags y opciones. Si el valor es un array, las claves válidas serán: filter que especifica el tipo filter type, flags que define cualquier flag que deba aplicarse a los filtros, y options que establece cualquier opción que se deba aplicar al filtro. Para entender mejor su funcionamiento, vea el ejemplo inferior.

Este parámetro puede ser tambien un integer indicando una constante de filtro.. Entonces, todos los valores en el array de entrada son filtrados por este filtro.

Valores devueltos

En caso de éxito un array que contiene los valores de las variables que se han pedido o FALSE en caso de fallo. El valor del array será FALSE si el filtro falla o NULL si la variable no está definida.

Ejemplos

Ejemplo #1 Ejemplo de filter_var_array()

<?php
error_reporting
(E_ALL E_STRICT);
$data = array(
    
'id_producto'    => 'libgd<script>',
    
'componente'     => '10',
    
'versiones'      => '2.0.33',
    
'test_escalar'    => array('2''23''10''12'),
    
'test_array'     => '2',
);

$args = array(
    
'id_producto'   => FILTER_SANITIZE_ENCODED,
    
'componente'    => array('filter'    => FILTER_VALIDATE_INT,
                            
'flags'     => FILTER_FORCE_ARRAY
                            
'options'   => array('min_range' => 1'max_range' => 10)
                           ),
    
'versiones'     => FILTER_SANITIZE_ENCODED,
    
'no_existe' => FILTER_VALIDATE_INT,
    
'test_scalar'   => array(
                            
'filter' => FILTER_VALIDATE_INT,
                            
'flags'  => FILTER_REQUIRE_SCALAR,
                           ),
    
'test_array'    => array(
                            
'filter' => FILTER_VALIDATE_INT,
                            
'flags'  => FILTER_FORCE_ARRAY,
                           )

);

$myinputs filter_var_array($data$args);

var_dump($myinputs);
echo 
"\n";
?>

El resultado del ejemplo sería:

array(6) {
  ["id_producto"]=>
  array(1) {
    [0]=>
    string(17) "libgd%3Cscript%3E"
  }
  ["componente"]=>
  array(1) {
    [0]=>
    int(10)
  }
  ["versiones"]=>
  array(1) {
    [0]=>
    string(6) "2.0.33"
  }
  ["no_existe"]=>
  NULL
  ["test_escalar"]=>
  bool(false)
  ["test_array"]=>
  array(1) {
    [0]=>
    int(2)
  }
}

Ver también



filter_var> <filter_list
[edit] Last updated: Fri, 25 May 2012
 
add a note add a note User Contributed Notes filter_var_array
xavier 18-Aug-2010 07:37
When I run the script on my linux box (php 5.2.10) the output of "Example #1 A filter_var_array() example"
is actually:

array(6) {
  ["product_id"]=>
  string(17) "libgd%3Cscript%3E"
  ["component"]=>
  array(1) {
    [0]=>
    int(10)
  }
  ["versions"]=>
  string(6) "2.0.33"
  ["doesnotexist"]=>
  NULL
  ["testscalar"]=>
  bool(false)
  ["testarray"]=>
  array(1) {
    [0]=>
    int(2)
  }
}

Notice that the values of "product_id" and "versions" are not arrays.  If you add the FILTER_FORCE_ARRAY flag to the "product_id" and "versions" filter arrays then the output returns as it is outlined in the example.
eguvenc at gmail dot com 06-Jun-2009 09:45
<?php
//an example of simply sanitize an array..

$data = array(
               
'<b>bold</b>',
               
'<script>javascript</script>',
               
'P*}i@893746%%%p*.i.*}}|.dw<?php echo "echo works!!";?>');

$myinputs = filter_var_array($data,FILTER_SANITIZE_STRING);

var_dump($myinputs);

//OUTPUT:
//formarray(3) { [0]=> string(4) "bold" [1]=> string(10) "javascript" [2]=> string(26) "P*}i@893746%%%p*.i.*}}|.dw" }
?>
Veysel zer 07-Jun-2007 09:41
Numeric keys are not allowed in the definition array

 
show source | credits | stats | sitemap | contact | advertising | mirror sites