Php learning notes filter

Source: Internet
Author: User
Php learning notes filter
  • Filter_var (): filter a single variable using a specified filter.
  • Filter_var (variable, filter, options): variable is required. optional. options specifies an array containing flag/option. Check the possible flags and options of each filter.

    $var=300;$int_options = array("options"=>array ( "min_range"=>0, "max_range"=>256 ));if(!filter_var($var, FILTER_VALIDATE_INT, $int_options)) { echo("Integer is not valid"); }else { echo("Integer is valid"); }?>
    Just like the code above, the options must be placed in a related array named "options. If a flag is used, it does not need to be in the array. Because the Integer is "300", it is not within the specified range, and the output of the above code will be "Integer is not valid ".


  • Filter_var_array (): filter multiple variables using the same or different filters.
  • Filter_input (): gets an input variable and filters it.
  • Filter_input (input_type, variable, filter, options)

    Input_type: INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_ENV, INPUT_SERVER, INPUT_SESSION (not yet inplemented), INPUT_REQUEST (not yet inplemented)

    Filter: (optional) ID of the filter to be used. The default value is FILTER_SANITIZE_STRING.

  • Filter_input_array (): gets multiple input variables and filters them using the same or different filters.
  • This function does not need to repeatedly call filter_input (), which is useful for filtering multiple input variables.

  • FILTER_VALIDATE_INT

    Verify the certificate value within the specified range

    FILTER_VALIDATE_BOOLEAN

    If it is "1", "true", "on", "yes", true is returned.
    If it is "0", "false", "off", or "no", false is returned.
    Otherwise, NULL is returned.

    FILTER_VALIDATE_FLOAT Verify value with floating point number
    FILTER_VALIDATE_URL Verify the value as a URL
    FILTER_VALIDATE_EMAIL Use the value as an email for verification.
    FILTER_VALIDATE_IP Verify the value as an IP address

    FILTER_SANITIZE_URL: The filter deletes all invalid URL characters in the string.


    There are two filters.

    Validating filter:

    Used to verify user input

    Strict format rules

    If the request succeeds, the expected type is returned. if the request fails, FALSE is returned.

    Sanitizing filter:

    Allows or disables specified characters in a string.

    No data format rules

    Always return Character creation


    Verification input:

    The first thing we need to do is to confirm whether the input data we are looking for exists.

    Then we use the filter_input () function to filter input data.

    If (! Filter_has_var (INPUT_GET, "email") {// check whether a variable of the specified input type exists.

    Echo ("Input type does not exist ");

    } Else {

    If (! Filter_input (INPUT_GET, "email", FILTER_VALIDATE_EMAIL) {// Obtain the input from outside the script and filter it out.

    Echo "E-Mail is not valid ";

    }

    }


    Purification input:

    First, check whether the input data we are searching for exists.

    Then, use the filter_input () function to purify the input data.

    If (! Filter_has_var (INPUT_POST, "url") {// checks whether the input variable url transmitted by the POST method exists.

    Echo ("Input type does not exist ");

    } Else {

    $ Url = filter_input (INPUT_POST, "url", FILTER_SANITIZE_URL); // If yes, cleanse (delete invalid characters) the file and store it in the $ url variable.

    }


    If the input variable is similar to the following: "http: // www. W3 non-o ol.com. c character n/", the $ url variable after cleaning should be like this:

    http://www.W3School.com.cn/

    Filter multiple inputs:

    A form is usually composed of multiple input fields. To avoid repeated calls to filter_var or filter_input, we can use filter_var_array or the filter_input_array function.


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


    $ Filters = array

    (

    "Name" => array

    (

    "Filter" => FILTER_SANITIZE_STRING

    ),

    "Age" => array

    (

    "Filter" => FILTER_VALIDATE_INT,

    "Options" => array

    (

    "Min_range" => 1,

    & Quot; max_range & quot; = & quot; 120 & quot;

    )

    ),

    "Email" => FILTER_VALIDATE_EMAIL,

    );


    $ Result = filter_input_array (INPUT_GET, $ filters );


    If (! $ Result ["age"])

    {

    Echo ("Age must be a number between 1 and 120.
    ");

    }

    Elseif (! $ Result ["email"])

    {

    Echo ("E-Mail is not valid.
    ");

    }

    Else

    {

    Echo ("User input is valid ");

    }

    ?>

    Explanation:

    1. set an array containing the name of the input variable and the filter used for the specified input variable.

    2. call the filter_input_array function. the parameters include GET input variables and the array you just set.

    3. check whether the age and email biarnewolf in the $ result variable have illegal input.


    Use Filter Callback:

    You can call a custom function and use it as a filter. In this way, we have full control over data filtering.


    Function convertSpace ($ string ){

    Return str_replace ("_", "", $ string );

    }


    $ String = "Peter_is_a_greate_boy! ";

    Echo filter_var ($ string, FILTER_CALLBACK, array ("options" => "convertSpace "));

    Explanation:

    1. create a function that replaces "_" with spaces.

    2. call the filter_var function. its parameter is the FILTER_CALLBACK filter and the array containing our function.




  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.