PHP advanced filter
Use filterDetect whether a number is within a range
The following example uses the filter_var() function to Check whether an INT type variable is within the range of 1 to 200:
array("min_range"=>$min, "max_range"=>$max))) === false) { echo("变量值不在合法范围内"); } else { echo("变量值在合法范围内"); } ?>
Program running result:
The variable value is within the legal range Inside
Detect IPv6 address
★FILTER_VALIDATE_IPThe filter validates the value as an IP.
The following example uses the filter_var() function to detect whether a $ip variable is an IPv6 address:
Program running result:
2001:0db8:85a3:08d3:1319:8a2e:0370:7334 is an IPv6 address
Detect URL - must contain QUERY_STRING (query string)
QUERY_STRINGLiteral meaning It is the query string. For example, common URL web addresses have xxx.asp?pn=123456? What follows the number is querystring
The following example uses the filter_var() function to detect whether $url contains the query string:
Program running result:
http://www.baidu.com is not a legal URL
above The Url URL in the example does not contain QUERY_STRING, so the running result is a negative answer
Remove characters with ASCII values greater than 127
★FILTER_SANITIZE_STRINGFilter removes or encodes unwanted characters.
The following example uses the filter_var() function to remove characters with an ASCII value greater than 127 in the string. It can also remove HTML tags:
Hello WorldÆØÅ!"; $newstr = filter_var($str, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH); echo $newstr; ?>
Program running results:
Hello World!
PHP Filter Reference Manual
You can too View the specific application of filters by visiting the PHP Filter Reference Manual on this site.
The reference manual contains a brief description and usage examples of filter parameters!