The phpfilter function library (extensions related to variables and types) can filter frequently used emails, IP addresses, variable arrays, etc? Introduction to the filter extension library? Thisextensionfiltersdatabyeithervalidatingorsanitizing php filter function library (extensions related to variables and types), which can filter frequently used emails, IP addresses, variable arrays, etc.
?
Introduction to the filter extension Library
?
This extension filters data by either validating or sanitizing it. this is especially useful when the data source contains unknown (or foreign) data, like user supplied input. for example, this data may come from an HTML form.
There are two main types of filtering:ValidationAndSanitization.
Validation is used to validate or check if the data meets certain qualifications. For example, passing inFILTER_VALIDATE_EMAIL
Will determine if the data is a valid email address, but will not change the data itself.
Sanitization will sanitize the data, so it may alter it by removing undesired characters. For example, passing inFILTER_SANITIZE_EMAIL
Will remove characters that are inappropriate for an email address to contain. That said, it does not validate the data.
FlagsAre optionally used with both validation and sanitization to tweak behaviour according to need. For example, passing inFILTER_FLAG_PATH_REQUIRED
While filtering an URLWill require a path (like/FooInHttp://example.org/foo) To be present.
?
This extended filter data passes verification and disinfection. This is especially useful when a data source contains unknown (or foreign) data, such as user input. For example, the data may come from an HTML form. There are two main types of filtering: verification and data disinfection. Verification is used to verify or check that data meets certain conditions. For example, when the email is verified in the filter, it is determined that if the data is a valid email address, but it does not change the data itself. Clean and sanitize the data, so it may change it by eliminating undesirable characters. For example, the characters deleted by filtering and purifying emails do not contain an email address. It is said that it does not verify the data. Flag is selectively used for verification and data disinfection to adjust behavior as needed. For example, passin
?
List of filtered functions:
Function name
Filter_list filter list -- returns all supported filters in a list.
Filter_id filter ID-the returned filter ID belongs to a name named filter
Filter_has_var filter already var-check if the specified type of variable exists
Filter_input filter input -- get the name of a specific external variable and filter it selectively
Filter_input_array filter input arrays -- obtain external variables and selectively filter them
Filter_var: use filter_var to filter the specified filter.
Filter_var_array filter var array -- get multiple variables and selectively filter them
?
?
?
?
Function example:
Filter_list
?
The output of the preceding routine is similar: array ([0] => int [1] => boolean [2] => float [3] => validate_regexp [4] => validate_url [5] => validate_email [6] => validate_ip [7] => string [8] => stripped [9] => encoded [10] => special_chars [11] => unsafe_raw [12] => email [13] => url [14] => number_int [15] => number_float [16] => magic_quotes [17] => callback)
?
? The combination of filter_list and filter_id
?
";print_r(filter_list());echo "
"; Foreach (filter_list () as $ key => $ value) {echo"
". $ Key." = ". $ value. '='. filter_id ($ value) ;}?> 0 = int = 2571 = boolean = 2582 = float = 2593 = validate_regexp = 2724 = validate_url = 2735 = validate_email = 2746 = validate_ip = 2757 = string = 5138 = stripped = 5139 = encoded = 51410 = special_chars = 51511 = unsafe_raw = 51612 = email = 51713 = url = 51814 = number_int = 51915 = number_float = 52016 = magic_quotes = 52117 = callback = 1024
?
? Filter_id
?
"; } ?> Will result in: boolean: 258 float: 259 validate_regexp: 272 validate_url: 273 validate_email: 274 validate_ip: 275 string: 513 stripped: 513 encoded: 514 special_chars: 515 unsafe_raw: 516 email: 517 url: 518 number_int: 519 number_float: 520 magic_quotes: 521 callback: 1024
?
?
Filter_has_var function efficiency:
To note: filter_has_var () is a bit faster than isset ()
Translation: the filter_has_var function is faster than isset ().
?
Please note that the function does not check the live array, it actually checks the content received by php:
Note: This function does not check including arrays, but only the content received by PHP.
?
?
would say "No", unless the parameter was actually in the querystring.Also, if the input var is empty, it will say Yes.
?
?
?
?
Verification Example 1 (verification email ):
?
? The above program output:
?
This (email_a) email address is considered valid.
?
Verification Example 2 (verify IP)
?
? The above program output:
?
This (ip_a) IP address is considered valid.
?
Example 3 (filter variables with options ):
?
array( 'min_range' => 0, 'max_range' => 3, ));if (filter_var($int_a, FILTER_VALIDATE_INT, $options) !== FALSE) { echo "This (int_a) integer is considered valid (between 0 and 3).\n";}if (filter_var($int_b, FILTER_VALIDATE_INT, $options) !== FALSE) { echo "This (int_b) integer is considered valid (between 0 and 3).\n";}if (filter_var($int_c, FILTER_VALIDATE_INT, $options) !== FALSE) { echo "This (int_c) integer is considered valid (between 0 and 3).\n";}$options['options']['default'] = 1;if (($int_c = filter_var($int_c, FILTER_VALIDATE_INT, $options)) !== FALSE) { echo "This (int_c) integer is considered valid (between 0 and 3) and is $int_c.";}?>
? The above program will output:
?
This (int_a) integer is considered valid (between 0 and 3).This (int_c) integer is considered valid (between 0 and 3) and is 1.
?
Remove harmful characters and verify Example 1:
?
? The above program will output:
?
This (a) sanitized email address is considered valid.This (b) sanitized email address is considered invalid.This (c) sanitized email address is considered valid.Before: (bogus@example.org)After: bogus@example.org
?
The following describes filter_input, which is taken from Baidu Encyclopedia:
?
Definition and usage
The filter_input () function obtains and filters the input from outside the script.
This function is used to verify variables from unsafe sources, such as user input.
This function can obtain input from various sources:
INPUT_GET
INPUT_POST
INPUT_COOKIE
INPUT_ENV
INPUT_SERVER
INPUT_SESSION (Not yet implemented)
INPUT_REQUEST (Not yet implemented)
If the operation succeeds, the filtered data is returned. if the operation fails, false is returned?Variable? If the parameter is not set, NULL is returned.
Syntax
Filter_input (input_type, variable, filter, options)
Parameter description
Input_type |
Required. Specifies the Input type. See the possible types in the list above. |
Variable |
Specifies the variable to be filtered. |
Filter |
Optional. Specifies the ID of the filter to be used. The default value is FILTER_SANITIZE_STRING .? See the complete PHP Filter function reference manual to obtain possible filters .? The filter ID can be an ID name (such as FILTER_VALIDATE_EMAIL) or an ID (such as 274 ). |
Options |
Specifies an array containing flag/option. Check the possible flags and options of each filter. |
Example
In this example, we use the filter_input () function to filter a POST variable. The accepted POST variable is a valid email address.
?
{Echo "E-Mail is not valid";} else {echo "E-Mail is valid" ;}?> The output is similar to: E-Mail is valid.
?
For more information about how to filter and verify strings, see the official PHP Manual.
When I wrote this article, I found that some of my friends had previously written it. can I find more links? Http://blog.johnsonlu.org/phpfilter/