-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10_filters_advanced.php
More file actions
99 lines (78 loc) · 2.39 KB
/
10_filters_advanced.php
File metadata and controls
99 lines (78 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
// 10_filters_advanced.php
// 📘 PHP Advanced Filters — Range Validation, IPv6, Query Required, and Strip High ASCII
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP Advanced Filters</title>
</head>
<body>
<h2>🧠 PHP Filters – Advanced Examples</h2>
<hr>
<?php
/*
------------------------------------------------------------
🔢 Example 1: Validate Integer in a Specific Range
------------------------------------------------------------
Checks if the integer is between 1 and 200 (inclusive)
*/
$int = 122;
$min = 1;
$max = 200;
echo "<h3>Integer Range Check (1–200):</h3>";
if (filter_var($int, FILTER_VALIDATE_INT, array("options" => array("min_range" => $min, "max_range" => $max))) === false) {
echo "❌ Variable value is NOT within the legal range.";
} else {
echo "✅ Variable value is within the legal range.";
}
?>
<hr>
<?php
/*
------------------------------------------------------------
🌐 Example 2: Validate an IPv6 Address
------------------------------------------------------------
Checks if the given IP is a valid IPv6 address
*/
$ip = "2001:0db8:85a3:08d3:1319:8a2e:0370:7334";
echo "<h3>IPv6 Address Validation:</h3>";
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.";
}
?>
<hr>
<?php
/*
------------------------------------------------------------
🔗 Example 3: Validate URL that Must Contain a Query String
------------------------------------------------------------
Checks if the URL includes a query (e.g., ?id=123)
*/
$url = "https://www.w3schools.com"; // No query string
echo "<h3>Validate URL with Query String:</h3>";
if (!filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_QUERY_REQUIRED) === false) {
echo "✅ $url is a valid URL with a query string.";
} else {
echo "❌ $url is NOT a valid URL with a query string.";
}
?>
<hr>
<?php
/*
------------------------------------------------------------
🔤 Example 4: Sanitize String and Remove High ASCII (>127)
------------------------------------------------------------
Removes:
- HTML tags
- Any character with ASCII > 127 (e.g., Æ, Ø, Å)
*/
$str = "<h1>Hello WorldÆØÅ!</h1>";
$newstr = filter_var($str, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
echo "<h3>Sanitized String (Removed HTML & High ASCII):</h3>";
echo $newstr; // Output: Hello World!
?>
</body>
</html>