PHP Learning Note--filter

Source: Internet
Author: User
Tags types of filters

    • 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.
<?php
$var =300; $int _options = Array ("options"=>array ("Min_range" =>0, "Max_range" =>256)); filter_var($var, FILTER_VALIDATE_INT, $int_options) {Echo ("Integer is not valid");} else {echo ("Integer is valid");}? >
"Options" in the relevant array. 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:A strict formatting rule used to validate user input returns the expected type if successful, or false if it failsSanitizing Filter:Used to allow or disallow characters specified in a string without a data format rule always returns 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 if there is a variable with 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 a POST method to transmit the input variable URL 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:
    <?php$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 ("must is a number between 1 and 120.<br/>");} ElseIf (! $result ["email"] {echo ("e-mail is not valid.<br/>");} else {echo ("User input is valid");}? > Explanation: 1. Set an array that contains the name of the input variable and the filter 2 for the specified input variable. Call the Filter_input_array function, which includes the get input variable and the array 3 that you just set. Detect $ The 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"); Explanation: 1. Create a function that replaces "_" with a space 2. Call Filter_ The 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.