Filter Multiple Inputs

Posted by tutor | Posted in PHP Tutorial | Posted on 17-12-2009-05-2008

0

A form generally consists of more than one input field. To avoid calling the filter_var or filter_input functions over and over again for each of the inputs, we can use the filter_var_array or the filter_input_array functions.

In this example we use the filter_input_array() function to filter three GET variables. The received GET variables is a name, an age and an e-mail address:

    <?php
    $filters = array
    (
    “name” => array
    (
    “filter”=>FILTER_SANITIZE_STRING
    ),
    “age” => array
    (
    “filter”=>FILTER_VALIDATE_INT,
    “options”=>array
    (
    “min_range”=>1,
    “max_range”=>120
    )
    ),
    “email”=> FILTER_VALIDATE_EMAIL,
    );
    $result = filter_input_array(INPUT_GET, $filters);
    if (!$result["age"])
    {
    echo(”Age must be a number between 1 and 120.<br />”);
    }
    elseif(!$result["email"])
    {
    echo(”E-Mail is not valid.<br />”);
    }
    else
    {
    echo(”User input is valid”);
    }
    ?>

The example above has three inputs (name, age and email) sent to it using the “GET” method. It works as:

  • Set an array containing the name of input variables and the filters used on the specified input variables
  • Call the filter_input_array() function with the GET input variables and the array we just set
  • Check the “age” and “email” variables in the $result variable for invalid inputs. (If any of the input variables are invalid, that input variable will be FALSE after the filter_input_array() function)
  • The second parameter of the filter_input_array() function can be an array or a single filter ID.

If the parameter is a single filter ID all values in the input array are filtered by the specified filter.

If the parameter is an array it must follow these rules:

  • Must be an associative array containing an input variable as an array key (like the “age” input variable)
  • The array value must be a filter ID or an array specifying the filter, flags and options

Using Filter Callback

You can also call your own function and use it as a filter using the FILTER_CALLBACK filter. This way, you can have full control of the data filtering.
The function you wish to use to filter is specified the same way as an option is specified. In an associative array with the name “options”

In the example below, we use a user created function to convert all “_” to whitespaces:

    <?php
    function convertSpace($string)
    {
    return str_replace(”_”, ” “, $string);
    }
    $string = “Peter_is_a_great_guy!”;
    echo filter_var($string, FILTER_CALLBACK,
    array(”options”=>”convertSpace”));
    ?>
    Output

    Peter is a great guy!

The example above converts all “_” to whitespaces. It works as:

  • Create a function to replace “_” to whitespaces
  • Call the filter_var() function with the FILTER_CALLBACK filter and an array containing our function

Write a comment

Twitter Users
Enter your personal information in the form or sign in with your Twitter account by clicking the button below.