WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP Filters Advanced


Validate an Integer Within a Range

The following model uses the filter_var() capacity to check if a variable is both of sort INT, and somewhere in the range of 1 and 200:

Example

<?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 esteem isn't inside the legitimate range");
} else {
    echo("Variable esteem is inside the legitimate range");
}
?>
Run model »

Validate IPv6 Address

The following model uses the filter_var() capacity to check if the variable $ip is a legitimate IPv6 address:

Example

<?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 legitimate IPv6 address");
} else {
    echo("$ip is certifiably not a legitimate IPv6 address");
}
?>
Run model »

Validate URL - Must Contain QueryString

The following model uses the filter_var() capacity to check if the variable $url is a URL with a querystring:

Example

<?php
$url = "http://www.welookups.com";

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

Remove Characters With ASCII Value > 127

The following precedent uses the filter_var() capacity to disinfect a string. It will both expel all HTML labels, and all characters with ASCII esteem > 127, from the string:

Example

<?php
$str = "<h1>Hello Worldæøå!</h1>";

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