This repository was archived by the owner on May 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.php
More file actions
93 lines (77 loc) · 1.86 KB
/
filter.php
File metadata and controls
93 lines (77 loc) · 1.86 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
<?php
/**
* Created by PhpStorm.
* User: pantao
* Date: 2017/12/29
* Time: 21:43
*/
echo "验证是否是整数。<br/>";
$int = 123;
if (filter_var($int, FILTER_VALIDATE_INT)) {
echo("Integer is valid");
} else {
echo("Integer is not valid");
}
echo "<br/><br/>区域验证<br/>";
$var = 300;
$int_options = array(
"options" => array
(
"min_range" => 0,
"max_range" => 256
)
);
if (!filter_var($var, FILTER_VALIDATE_INT, $int_options)) {
echo("Integer is not valid");
} else {
echo("Integer is valid");
}
echo "<br/><br/>输入验证<br/>";
if (!filter_has_var(INPUT_GET, "email")) {
echo("Input type does not exist");
} else {
if (!filter_input(INPUT_GET, "email", FILTER_VALIDATE_EMAIL)) {
echo "E-Mail is not valid";
} else {
echo "E-Mail is valid";
}
}
echo "<br/><br/>净化输入<br/>";
if (!filter_has_var(INPUT_POST, "url")) {
echo("Input type does not exist");
} else {
$url = filter_input(INPUT_POST, "url", FILTER_SANITIZE_URL);
}
echo "<br/><br/>过滤多个输入<br/>";
$filters = array
(
"name" => array
(
"filter" => FILTER_SANITIZE_STRING
),
"age" => array
(
"filter" => FILTER_VALIDATE_INT,
"options" => array
(
"min_range" => 1,
"max_range" => 120
)
),
"email" => FILTER_VALIDATE_EMAIL,
);
$result = filter_input_array(INPUT_GET, $filters);
if (!$result["age"]) {
echo("Age must be a number between 1 and 120.<br />");
} elseif (!$result["email"]) {
echo("E-Mail is not valid.<br />");
} else {
echo("User input is valid");
}
echo "<br/><br/>Filter Callback自定义过滤<br/>";
function convertSpace($string)
{
return str_replace("_", " ", $string);
}
$string = "Peter_is_a_great_guy!";
echo filter_var($string, FILTER_CALLBACK, array("options" => "convertSpace"));