The following example uses the filter_var() function to check whether an INT type variable is between 1 and 200:
<?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("The variable value is not within the legal range");} else { echo("The variable value is within the legal range");}?>
The following example uses the filter_var() function to check whether an $ip variable is an IPv6 address:
<?php$ip = "2001:0db8:85a3:08d3:1319:8a2e:0370:7334";if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) { echo("$ip is an IPv6 address");} else { echo("$ip is not an IPv6 Address");}?>
The following example uses the filter_var() function to detect whether $url contains the query string:
<?php$url = "http://www.codercto.com";if (!filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_QUERY_REQUIRED) === false) { echo("$url is a legal URL");} else { echo("$url is not a valid URL");}?>
The following example uses the filter_var() function to remove characters with an ASCII value greater than 127 in a string. It can also remove HTML tags:
<?php$str = "<h1>Hello WorldÆØÅ!</h1>";$newstr = filter_var($str, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);echo $newstr;?>
You can also view the specific application of filters by visiting the PHP Filter Reference Manual on this site.
The reference manual contains a brief description of the filter parameters and usage examples!