Phpfilter (2)

Source: Internet
Author: User
Phpfilter (2) Boolean verify FILTER_VALIDATE_BOOLEAN

1 
  

The above code outputs 1. because the filter finds a valid Boolean value, the following lists other values that can return true.
1
"1"
"Yes"
"True"
"On"
TRUE
The following values return false.
0
"0"
"No"
"False"
"Off"
""
NULL
FALSE
The following operations are also supported:

1 
 

In the above code, the in_array function is successfully executed, and true is returned. Therefore, the final code outputs true.
We can also pass an array to determine the boolean type of the value in the array.

 1 
 

The above code output is as follows:
Array (6 ){
[0] => bool (false)
[1] => bool (true)
[2] => bool (false)
[3] => bool (false)
[4] => bool (false)
[5] => array (5 ){
[0] => bool (false)
[1] => bool (true)
[2] => bool (false)
[3] => bool (false)
[4] => bool (false)
}
}

Floating point verification FILTER_VALIDATE_FLOAT

 1 
  

Like other verifications, you can also perform floating point verification on an array. Similar to boolean verification, flgs FILTER_REQUIRE_ARRAY is provided.

/*** An array of values ***/
$ Array = array (1.2, "1.7", "", "-12345.678", "some text", "abcd4.2efgh", array ());

/*** Validate the array ***/
$ Validation_array = filter_var ($ array, FILTER_VALIDATE_FLOAT, FILTER_REQUIRE_ARRAY );

/*** Dump the array of validated data ***/
Var_dump ($ validation_array );

?>
The above code output is as follows:
Array (7 ){
[0] => float (1.2)
[1] => float (1.7)
[2] => bool (false)
[3] => float (-23234.123)
[4] => bool (false)
[5] => bool (false)
[6] => array (0 ){}
}
The floating point filter supports specifying a delimiter between numbers.

 1 
  ",", 6 "1.234" => "..", 7 "1.2e3" => "," 8 ); 9 10 /*** validate the floats against the user defined decimal seperators ***/11 foreach ($floats as $float => $dec_sep)12 {13     $out = filter_var($float, FILTER_VALIDATE_FLOAT,     array("options"=>array("decimal" => $dec_sep)));14     /*** dump the results ***/15     var_dump($out);16 }17 ?>

In the code above, the first element value in the $ floats function is ',', so it is specified with the separator ',' when determining the 1,234 value, so true is returned.
Complete return value of the above code
Float (1.234)
Warning: filter_var () [function. filter-var]: decimal separator must be one char in/www/filter. php on line 13
Bool (false)
Bool (false)

Verify URL FILTER_VALIDATE_URL
URL verification is very difficult. due to the uncertainty of the URL, there is no limit on the maximum length, and its format is diversified, you can read RFC 1738 to learn about the URL. Then you can create a class to verify all ipv4 and ipv6 URLs and some other URLs. You can also simply use FILTER_VALIDATE_URL to verify the URL.

 1 
 ";16 }17 ?>

In the above example, a simple if statement is used to determine whether the given URL is legal, but not all URLs are in this format. Sometimes a URL can be an IP address, or multiple parameters may be passed in the URL. Below we provide several flags to help us verify the URL
FILTER_FLAG_SCHEME_REQUIRED-The URL must be RFC-compatible. (For example, http: // example)
FILTER_FLAG_HOST_REQUIRED-requires that the URL contain the host name (http://www.example.com)
FILTER_FLAG_PATH_REQUIRED-The URL must exist after the host name (for example, eg.com/example1 /)
FILTER_FLAG_QUERY_REQUIRED-requires the URL to have a query string (for example, "eg. php? Age = 37 ")

 1 
 

It can be found that the above code has not passed verification

IP filter FILTER_VALIDATE_IP
The FILTER_VALIDATE_IP filter verifies the value as an IP address.
Name: "validate_ip"
ID-number: 275
Possible flag:
FILTER_FLAG_IPV4-the required value is a valid IPv4 IP address (for example, 255.255.255.255)
FILTER_FLAG_IPV6-the required value is a valid IPv6 IP address (for example, 2001: 0db8: 85a3: 08d3: 1319: 8a2e: 0370: 7334)
FILTER_FLAG_NO_PRIV_RANGE-the request value is the private domain IP address specified by RFC (for example, 192.168.0.1)
FILTER_FLAG_NO_RES_RANGE-the value is not within the reserved IP address range. This flag accepts IPV4 and IPV6 values.

Email Filter FILTER_VALIDATE_EMAIL
The FILTER_VALIDATE_EMAIL filter uses the value as the email address for verification.

 1 
 

Custom filter FILTER_CALLBACK

The FILTER_CALLBACK filter uses user-defined functions to filter values.
This filter provides us with full control over data filtering.
The specified function must be stored in the associated array named "options.

 1 
 "convertSpace"));11 ?>

Output

Peter_is_a_great_guy!


PHP technology exchange group 170855791

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.