Filter_var (variable, filter, options): variable is required. optional. options specifies an array containing flag/option. Check the possible flags and options of each filter.
Input_type: INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_ENV, INPUT_SERVER, INPUT_SESSION (not yet inplemented), INPUT_REQUEST (not yet inplemented)
Filter: (optional) ID of the filter to be used. The default value is FILTER_SANITIZE_STRING.
This function does not need to repeatedly call filter_input (), which is useful for filtering multiple input variables.
|
FILTER_VALIDATE_INT |
Verify the certificate value within the specified range |
|
FILTER_VALIDATE_BOOLEAN |
If it is "1", "true", "on", "yes", true is returned.
If it is "0", "false", "off", or "no", false is returned.
Otherwise, NULL is returned. |
| FILTER_VALIDATE_FLOAT |
Verify value with floating point number |
| FILTER_VALIDATE_URL |
Verify the value as a URL |
| FILTER_VALIDATE_EMAIL |
Use the value as an email for verification. |
| FILTER_VALIDATE_IP |
Verify the value as an IP address |
FILTER_SANITIZE_URL: The filter deletes all invalid URL characters in the string.
There are two filters.
Validating filter:
Used to verify user input
Strict format rules
If the request succeeds, the expected type is returned. if the request fails, FALSE is returned.
Sanitizing filter:
Allows or disables specified characters in a string.
No data format rules
Always return Character creation
Verification input:
The first thing we need to do is to confirm whether the input data we are looking for exists.
Then we use the filter_input () function to filter input data.
If (! Filter_has_var (INPUT_GET, "email") {// check whether a variable of the specified input type exists.
Echo ("Input type does not exist ");
} Else {
If (! Filter_input (INPUT_GET, "email", FILTER_VALIDATE_EMAIL) {// Obtain the input from outside the script and filter it out.
Echo "E-Mail is not valid ";
}
}
Purification input:
First, check whether the input data we are searching for exists.
Then, use the filter_input () function to purify the input data.
If (! Filter_has_var (INPUT_POST, "url") {// checks whether the input variable url transmitted by the POST method exists.
Echo ("Input type does not exist ");
} Else {
$ Url = filter_input (INPUT_POST, "url", FILTER_SANITIZE_URL); // If yes, cleanse (delete invalid characters) the file and store it in the $ url variable.
}
If the input variable is similar to the following: "http: // www. W3 non-o ol.com. c character n/", the $ url variable after cleaning should be like this:
http://www.W3School.com.cn/
Filter multiple inputs:
A form is usually composed of multiple input fields. To avoid repeated calls to filter_var or filter_input, we can use 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 email address:
$ Filters = array
(
"Name" => array
(
"Filter" => FILTER_SANITIZE_STRING
),
"Age" => array
(
"Filter" => FILTER_VALIDATE_INT,
"Options" => array
(
"Min_range" => 1,
& Quot; max_range & quot; = & quot; 120 & quot;
)
),
"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. set an array containing the name of the input variable and the filter used for the specified input variable.
2. call the filter_input_array function. the parameters include GET input variables and the array you just set.
3. check whether the age and email biarnewolf in the $ result variable have illegal input.
Use Filter Callback:
You can call a custom function and use it as a filter. In this way, we have full control over 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 spaces.
2. call the filter_var function. its parameter is the FILTER_CALLBACK filter and the array containing our function.