Validate an Integer Within a Range
<!DOCTYPE html> <html> <body> <?php /* variable to check */ $int = 12; /* min value */ $min = 1; /* max value */ $max = 20; if (filter_var($int, FILTER_VALIDATE_INT, array("options" => array("min_range"=>$min, "max_range"=>$max))) === false) { echo("Variable value is not within the range"); } else { echo("Variable value is within the range"); } ?> </body> </html>
Validate IPv6 Address
IPv6 addresses are normally written as eight groups of four hexadecimal digits, where each group is separated by a colon (:).
<!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>
Validate URL – Must Contain QueryString
<!DOCTYPE html> <html> <body> <?php // Variable to check $url = "http://scriptsgurus.com/"; // Validate url 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"); } ?> </body> </html>
Remove Characters With ASCII Value > 127
<!DOCTYPE html> <html> <body> <?php // Variable to check $str = "<h1>Hello WorldÆØÅ!</h1>"; // Remove HTML tags and all characters with ASCII value > 127 $newstr = filter_var($str, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH); echo $newstr; ?> </body> </html>