In this example to use filter_var() to check if a variable is both of type is Int and lies between 1 to 200:
Output :Variable value is within the legal range.
In this Example the filter_var()
function to check if the variable $ip is a valid IPv6 address:
<!DOCTYPE html>
<html>
<body>
<?php
// Variable to check
$ip = "2001:0db8:85a3:08d3:1319:8a2e:0370:7334";
// Validate ip as IPv6
if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) {
echo("$ip is a valid IPv6 address");
} else {
echo("$ip is not a valid IPv6 address");
}
?>
</body>
</html>
Ouput :
2001:0db8:85a3:08d3:1319:8a2e:0370:7334 is a valid IPv6 address
The following example uses the filter_var()
function to sanitize a string. It will both remove all HTML tags, and all characters with ASCII value > 127, from the string:
<!DOCTYPE html>
<html>
<body>
<?php
$str = "<h1>Hello WorldÆØÅ!</h1>";
$newstr = filter_var($str, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
echo $newstr;
?>
</body>
</html>
OutPut: Hello World!