Latest web development tutorials

PHP Advanced Filters

Detecting whether a number in the range of a

The following example uses filter_var () function to detect whether a variable of type INT at 1-200 within:

Examples

<? Php
$ Int = 122;
$ Min = 1;
$ Max = 200;

if (filter_var ($ int, FILTER_VALIDATE_INT, array ( "options" => array ( "min_range" => $ min, "max_range" => $ max))) === false) {
echo ( "variable value is not within the legal range");
} Else {
echo ( "variable value is within the legitimate scope");
}
?>

try it"

Detection IPv6 address

The following example uses filter_var () function to detect whether a variable is $ ip IPv6 address:

Examples

<? Php
$ Ip = "2001: 0db8: 85a3: 08d3: 1319: 8a2e: 0370: 7334";

if (! filter_var ($ ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) {
echo ( "$ ip is a IPv6 address");
} Else {
echo ( "$ ip is not an IPv6 address");
}
?>

try it"

Detection URL - must contain the QUERY_STRING (query string)

The following example uses filter_var () function to detect $ url contains query string:

Examples

<? Php
$ Url = "http://www.w3big.com";

if (! filter_var ($ url, FILTER_VALIDATE_URL, FILTER_FLAG_QUERY_REQUIRED) === false) {
echo ( "$ url is a valid URL");
} Else {
echo ( "$ url is not a valid URL");
}
?>

try it"

Remove ASCII values ​​greater than 127 characters

The following example uses filter_var () function to remove the string ASCII values ​​greater than 127 characters, it can also remove HTML tags:

Examples

<? Php
$ Str = "<h1> Hello WorldÆØÅ </ h1>!";

$ Newstr = filter_var ($ str, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
echo $ newstr;
?>

try it"

PHP Filter Reference Manual

You can also visit the site of the PHP filter reference manual to see the specific application of the filter.

Reference manual contains a brief description of the filter parameters and usage examples!