PHP Learning notes Filter

Source: Internet
Author: User
  • Filter_var (): Filters a single variable with a specified filter
  • Filter_var (variable,filter,options): variable required, filter optional, options specify an array containing the flags/options. Check the possible flags and options for 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 called options. If you use flags, you do not need to be inside the array. Since the integer is "300", it does not exist in the specified range, the output of the above code will be "Integer is not valid".


  • Filter_var_array (): Filter multiple variables with 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, specifies the ID of the filter to be used. The default is filter_sanitize_string.

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

  • Filter_validate_int

    Validates a value with a certificate within a specified range

    Filter_validate_boolean

    Returns True if "1", "true", "on", "Yes"
    Returns False if "0", "false", "off", "no"
    Otherwise, NULL is returned

    Filter_validate_float Verifying values with floating-point numbers
    Filter_validate_url Verify the value as a URL
    Filter_validate_email Verify the value as an e-mail
    Filter_validate_ip Verify the value as an IP address

    Filter_sanitize_url: The filter removes all illegal URL characters from the string.


    There are two types of filters.

    Validating filter:

    Used to validate user input

    Strict formatting rules

    Returns the expected type if successful, or false if it fails

    Sanitizing Filter:

    Used to allow or disallow characters specified in a string

    Unlimited data formatting rules

    Always return character creation


    Verify Input:

    The first thing we need to do is confirm the existence of the input data we're looking for.

    We then use the Filter_input () function to filter the input data.

    if (!filter_has_var (input_get, "email")) {//check for the existence of a variable of the specified input type

    Echo ("Input type does not exist");

    }else{

    if (! Filter_input (input_get, "email", filter_validate_email)) {//Get input from outside the script and filter

    echo "e-mail is not valid";

    }

    }


    Purifying Input:

    First confirm that there is the input data we are looking for.

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

    if (!filter_has_var (input_post, "url")) {//detects if there is an input variable URL for POST method transfer

    Echo ("Input type does not exist");

    }else{

    $url =filter_input (input_post, "url", filter_sanitize_url);//If present, purify (remove illegal characters) and present it in the

    }


    If the input variable is similar to this: "Http://www.W3 non-O-ol.com.c character n/", then the purified $url variable should be this:

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

    Filter Multiple Inputs:

    A form is typically composed of multiple input fields. To avoid repeating calls to Filter_var or filter_input, we can use the 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 e-mail address:


    $filters = array

    (

    "Name" = = Array

    (

    "Filter" =>filter_sanitize_string

    ),

    "Age" = = array

    (

    "Filter" =>filter_validate_int,

    "Options" =>array

    (

    "Min_range" =>1,

    "Max_range" =>120

    )

    ),

    "e-mail" = Filter_validate_email,

    );


    $result = Filter_input_array (Input_get, $filters);


    if (! $result ["Age"])

    {

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

    }

    ElseIf (! $result ["email"])

    {

    Echo ("e-mail is not valid.
    ");

    }

    Else

    {

    Echo ("User input is valid");

    }

    ?>

    Explain:

    1. Set an array that contains the name of the input variable and the filter for the specified input variable

    2. Call the Filter_input_array function, which includes the get input variable and the array you just set

    3. Detect if age and email in the $result variable have illegal input than the wolf.


    using the filter Callback:

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


    function Convertspace ($string) {

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

    }


    $string = "peter_is_a_greate_boy!";

    Echo Filter_var ($string, Filter_callback,array ("Options" = "convertspace"));

    Explain:

    1. Create a function to replace "_" with a space

    2. Call the Filter_var function, whose arguments are the Filter_callback filter and the array that contains our functions




  • 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.