Filter_var (): Filters a single variable Filter_var (variable,filter,options) via a specified filter: Variable required, filter optional, options specify an array containing the logo/option. 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");}?>Like the code above, the option must be placed in an associated array named "Options." If you use a flag, you do not need to be inside the array. Because the integer is "300", it is outside the specified range, the output of the above code will be "integer is not valid".
|
Filter_var_array (): Filter multiple variables through the same or different filters Filter_input (): Get an input variable and filter 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: Optionally, specify the ID of the filter to use. The default is filter_sanitize_string. Filter_input_array (): Get multiple input variables and filter them through the same or different filters This function is useful for filtering multiple input variables without having to call Filter_input () repeatedly.
| 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 return null |
| Filter_validate_float |
Validating values with floating-point numbers |
| Filter_validate_url |
Validate the value as a URL |
| Filter_validate_email |
Verify the value as an e-mail |
| Filter_validate_ip |
Validate the value as an IP address |
Filter_sanitize_url: Filter deletes all illegal URL characters in the string.
There are two kinds of filters.
Validating filter:To verify that the user enters a strict format rule returns the expected type if successful and false if it fails
Sanitizing Filter:Used to allow or disallow characters specified in a string no data formatting rules always return character creation
Validate input:The first thing we need to do is confirm that we have the input data we're looking for. Then we use the Filter_input () function to filter the input data. if (!filter_has_var (input_get, "email")) {//Check for variable echo ("input type does not exist") that specifies the input type;} 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 verify that the input data we are looking for exists. Then, the Filter_input () function is used to purify the input data. if (!filter_has_var (input_post, "url")) {//detect the presence of the input variable URL echo ("input type does not exist") for POST method delivery;} else{$url =filter_input (input_post, "url", filter_sanitize_url);//If present, purify it (remove illegal characters) and present it in $url variable}
If the input variable is like this: "Http://www.W3 ol.com.c character n/", then the purified $url variable should be:
http://www.W3School.com.cn/
Filter Multiple Inputs:A form is usually made up of multiple input fields. To avoid repeated calls to Filter_var or filter_input, we can use the Filter_var_array or the Filter_input_array function.
In this case, we use the Filter_input_array () function to filter three get variables. The receive variable received is a name, an age, and an e-mail address:
-->Array ("filter" =>filter_sanitize_string), "Age" => Array ("Filter" =>filter_validate_int, "Options" => Array ("Min_range" =>1, "Max_range" =>120)), "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. Sets an array containing the name of the input variable and the filter 2 for the specified input variable. Call the Filter_input_array function, The parameters include the get input variable and the array 3 that you just set. Detect age and email in $result variables is there any illegal input to the alpha 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 the Filter_var function, whose arguments are filter_callback filters and an array containing our functions