Php learning notes-filter. Php learning notes -- filterfilter_var (): filter a single variable filter_var (variable, filter, options) using a specified filter: variable is required, and filter is optional, options rules include php learning notes -- filter
Filter_var (): Use a specified filter to filter a single variable filter_var (variable, filter, options): variable is required, and filter is optional. options specifies an array containing flag/option. Check the possible flags and options of 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") ;}?>
Just like the code above, the options must be placed in a related array named "options. If a flag is used, it does not need to be in the array. Because the Integer is "300", it is not within the specified range, and the output of the above code will be "Integer is not valid ". |
Filter_var_array (): filter multiple variables using 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: (optional) ID of the specified filter. The default value is FILTER_SANITIZE_STRING. Filter_input_array (): gets multiple input variables and filters them using 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 |
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 the strict format rules entered by the user. if successful, the expected type is returned. if failed, FALSE is returned.
Sanitizing filter:It is used to allow or prohibit the specified character in a string from having no data format rules.
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 the specified Input type variable 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 the 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 echo ("Input type does not exist") transmitted by the POST method exists ");} else {$ url = filter_input (INPUT_POST, "url", FILTER_SANITIZE_URL); // If yes, purify it (delete invalid characters) and store it in $ 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:
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. set an array containing the name of the input variable and the filter used for the specified input variable. call the filter_input_array function. the parameters include the GET input variable and the array you just set. checks whether age and email biarwolf 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.
Optional filter_var (): uses a specified filter to filter a single variable filter_var (variable, filter, options): Required for variable, optional for filter, and the options rule contains the mark...