The PHP filter contains two types of Validation: used to verify that the verification item is valid Sanitization: used to format the verified item, so it may modify the value of the verification item, delete invalid characters. PHP filters include two types
Validation: used to verify that the verification item is valid
Sanitization: used to format the verified project. Therefore, it may modify the value of the verification item and delete invalid characters.
Input_filters_list ()
Used to list all filters supported by the current system.
$filter){ echo $filter.' '.filter_id($filter)."\n";}?>
The above code will output the following information:
Filter Name
Filter ID
Int 257
Boolex 258
Float 259
Validate_regexp 272
Validate_url 273
Validate_email 274
Validate_ip 275.
String 513
Stripped 513
Encoded 514.
Special_chars 515
Full_special_chars 522
Unsafe_raw 516
Email 517
Urls 518
Number_int 519
Number_float 520
Magic_quotes 521
Callback 1024
Each filter has a unique ID. Each filter can be used by the filter_var () function. The following describes how to use it one by one. Note that the preceding string and strippedID are the same because they are the same filter or two aliases of the same filter.
Filter data
Use the filter_var () method to filter data. The following is a simple filtering example.
The code above will data an integer of 1234, because the $ int variable is verified by the integer type, this time change the content of the $ int variable
At this time, the code is running and no variable output is found. this is because the $ in variable is not verified, so this method returns bool (false ). Note that bool (false) is returned even if $ int =)
Integer verification
The above code simply verifies whether the given value is an integer. In fact, FILTER_VALIDATE_INT also provides a value range verification. next we will verify a variable, determine whether it is an integer, and verify whether its value is between 50 and 100.
$min, "max_range" => $max)); //42?>
Running the code above, I found 42 was output, and no error was found. why? When you want to add a verification rule to the verification, you must pass an array containing the 'options' key as follows:
array("min_range" => $min, "max_range" => $max)));?>
When you run the code above, no output is displayed on the page. if false is returned, the verification is successful.
This method can also be used to verify the range of negative numbers.
At the same time, this method also supports single-range values, that is, only specifying a maximum or minimum value range, such:
array('min_range' => $min))); //12?>
The above code verifies whether $ int is an integer value greater than (not including equal to) $ min. run the code and output 12
Validate a group of variables
The above examples are just simple verification of a single value. what if we verify a group of variables? The answer is filter_var_array (). This function can verify multiple data types at the same time. Here is a simple example:
$value) { echo $key.' -- '.$value.'
'; }?>
Run the above code and output the following:
0 -- 101 -- 1092 -- 3 -- -12344 -- 5 -- 6 -- Array
Octal and hexadecimal
The FILTER_VALIDATE_INT filter supports both octal and hexadecimal. The two flags are:
FILTER_FLAG_ALLOW_HEX
FILTER_FLAG_ALLOW_OCTAL
Pass flags using arrays
FILTER_FLAG_ALLOW_HEX)); //255?>
Boolean verify FILTER_VALIDATE_BOOLEAN
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:
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.
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
Float array verification
Like other verifications, you can also perform floating point verification on an array. Similar to boolean verification, flgs FILTER_REQUIRE_ARRAY is provided.
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.234" => "..", "1.2e3" => "," ); /*** validate the floats against the user defined decimal seperators ***/ foreach ($floats as $float => $dec_sep) { $out = filter_var($float, FILTER_VALIDATE_FLOAT, array("options" => array("decimal" => $dec_sep))); /*** dump the results ***/ var_dump($out); }?>
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 13bool(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.
"; }?>
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. The following provides several flags to help us verify the URL:
FILTER_FLAG_SCHEME_REQUIRED-The URL must be RFC-compatible. (For example: http://php1.cn)
FILTER_FLAG_HOST_REQUIRED-requires that the URL contain the host name (for example, the http://levi.php1.cn)
FILTER_FLAG_PATH_REQUIRED-requires that the URL exist after the host name (for example, http://levi.php1.cn/test/phpmailer)
FILTER_FLAG_QUERY_REQUIRED-requires the URL to have a query string (for example: http://levi.php1.cn /? P = 2618)
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.
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.
"convertSpace"));?>
Output
Peter_is_a_great_guy!