PHP Development Basics Tutorial Advanced Filters
1. Check whether a number is within a range
The following example uses the filter_var() function to detect whether an INT type variable is between 1 and Within 200:
The code is as follows
<?php $int = 134; $min = 1; $max = 200; if (filter_var($int, FILTER_VALIDATE_INT, array("options" => array("min_range"=>$min, "max_range"=>$max))) === false) { echo("變量值不在合法范圍內(nèi)"); } else { echo("變量值在合法范圍內(nèi)"); } ?>
The output is shown in the picture on the right
2. Detect IPv6 address
The following example uses the filter_var() function to detect whether a $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 是一個 IPv6 地址"); } else { echo("$ip 不是一個 IPv6 地址"); } ?>
The output is shown on the right
three , Detect URL - must contain QUERY_STRING (query string)
The following example uses the filter_var() function to detect whether $url contains the query string:
The code is as follows
<?php $url = "http://miracleart.cn"; if (!filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_QUERY_REQUIRED) === false) { echo("$url 是一個合法的 URL"); } else { echo("$url 不是一個合法的 URL"); } ?>
The output result is shown in the picture on the right
4. Remove characters with ASCII values ??greater than 127
Move Except characters with ASCII value greater than 127
The code is as follows
<?php $str = "<h1>Hello World???!</h1>"; $newstr = filter_var($str, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH); echo $newstr; ?>
The output result is shown in the picture on the right
Note: For more PHP filter applications, please see the PHP manual