Looks like filter_input_array isn't aware of changes to the input arrays that were made before calling filter_input_array. Instead, it always looks at the originally submitted input arrays.
So this will not work:
$_POST['my_float_field'] = str_replace(',','.',$_POST['my_float_field']);
$args = array('my_float_field',FILTER_VALIDATE_FLOAT);
$result = filter_input_array(INPUT_POST, $args);
filter_input_array
(PHP 5 >= 5.2.0, PECL filter:0.11.0)
filter_input_array — Obtiene múltiples variables desde afuera de PHP y opcionalmente las filtra
Descripción
Esta función es útil para recuperar varios valores sin llamar filter_input() repetitivamente.
Lista de parámetros
- tipo
-
Un valor entre INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, INPUT_ENV, INPUT_SESSION, o INPUT_REQUEST.
- definicion
-
Una matriz que define los argumentos. Una clave válida es un valor tipo string que contenga un nombre de variable, y un valor válido es un tipo de filtro, o un valor tipo array que especifique el filtro, las banderas y las opciones. Si el valor es una matriz, las claves válidas son filter, que especifica el tipo de filtro, flags que especifica las banderas que se apliquen al filtro, y options que especifica las opciones del filtro. Vea el ejemplo a continuación para más detalles.
Este parámetro puede ser también un entero que contenga una constante de filtro. Entonces todos los valores en la matriz de entrada son filtrados por este filtro.
Valores retornados
Una matriz que contiene los valores de las variables solicitadas en caso de éxito, o FALSE si ocurre un error. Un valor de matriz será FALSE si el filtro falla, o NULL si la variable no es establecida. O si la bandera FILTER_NULL_ON_FAILURE es usada, devuelve FALSE si la variable no es definida y NULL si el filtro falla.
Ejemplos
Example #1 Un ejemplo de filter_input_array()
<?php
error_reporting(E_ALL | E_STRICT);
/* los datos vinieron realmente desde POST
$_POST = array(
'id_producto' => 'libgd<script>',
'componente' => '10',
'versiones' => '2.0.33',
'prueba_escalar' => array('2', '23', '10', '12'),
'prueba_matriz' => '2',
);
*/
$args = array(
'id_producto' => FILTER_SANITIZE_ENCODED,
'componente' => array('filter' => FILTER_VALIDATE_INT,
'flags' => FILTER_REQUIRE_ARRAY,
'options' => array('min_range' => 1, 'max_range' => 10)
),
'versiones' => FILTER_SANITIZE_ENCODED,
'no_existe' => FILTER_VALIDATE_INT,
'prueba_escalar' => array(
'filter' => FILTER_VALIDATE_INT,
'flags' => FILTER_REQUIRE_SCALAR,
),
'prueba_matriz' => array(
'filter' => FILTER_VALIDATE_INT,
'flags' => FILTER_REQUIRE_ARRAY,
)
);
$mis_entradas = filter_input_array(INPUT_POST, $args);
var_dump($mis_entradas);
echo "\n";
?>
El resultado del ejemplo seria:
array(6) { ["id_producto"]=> string(17) "libgd%3Cscript%3E" ["componente"]=> int(10) ["versiones"]=> string(6) "2.0.33" ["no_existe"]=> NULL ["prueba_escalar"]=> bool(false) ["prueba_matriz"]=> array(1) { [0]=> int(2) } }
filter_input_array
08-Jul-2008 03:37
10-Sep-2007 07:32
The above example will actually output "NULL" because of the undefined variable doesnotexist - see http://bugs.php.net/bug.php?id=42608.
22-Aug-2007 07:10
extract() is a very convenient way of copying all those variables to the local scope. (see http://www.php.net/extract)
08-Jun-2007 10:02
The above example raises other questions such as how one would validate an html array. In the input form each input tag that refers to an html array would be named for example testarray[]. However, after the form is submitted, the syntax for validating the values is different from the expected $_POST['testarray[]']. Instead one has to drop the braces and validate as follows, assuming that testarray[] is supposed to be an html array of numerical values:
Valid test:
echo '*';
echo filter_input(
INPUT_POST,
'testarray',
FILTER_VALIDATE_INT,
FILTER_REQUIRE_ARRAY
);
echo '*';
But the following is an invalid test that results in 2 consequtive asterisks only!
echo '*';
echo filter_input(INPUT_POST,
'testarray[]',
FILTER_VALIDATE_INT,
FILTER_REQUIRE_ARRAY
);
echo '*';
So, there is a naming inconsistency going on, as after the form is submitted, one has to forget about the original name of the submitted array by dropping its braces. Maybe when the PECL/Filter extension is reviewed again, the great ones might consider making the syntax a little more forgiving.
