sugiphp / filter
过滤器辅助函数
1.0.0
2014-12-16 14:18 UTC
Requires
- php: >=5.3
This package is not auto-updated.
Last update: 2024-09-14 15:52:44 UTC
README
简单的过滤器辅助函数。
整数
验证在 $min 和 $max 范围内的整数值(如果它们不是 false)。如果给定的值不是整数或超出范围,则返回默认值。
<?php Filter::int($value, $min = false, $max = false, $default = false); // Examples: Filter::int(0); // 0 Filter::int(""); // false Filter::int(1.0); // 1 Filter::int(1.1); // false Filter::int(1, 2); // false - outside the minimum range Filter::int(5, 2, 4); // false - outside maximum allowed value Filter::int("1"); // 1 Filter::int("1.0"); // false Filter::int("1a"); // false Filter::int("hi", false, false, 77); // 77 - Returns the default value ?>
通常,开发者需要验证用户提供的某个值,因此上述示例很少使用。取而代之的是,更常用的是几个用于整数验证的过滤器。
<?php // Validate integer from GET parameter - $_GET["key"]. // Default value is returned if the key is not found, or cannot be converted to an integer, // or the value is outside the min / max range. Filter::intGet($key, $min_range = false, $max_range = false, $default = false); // for example if the URL is http://example.com?page=12 Filter::intGet("page", 1, false, 1); // returns 12 Filter::intGet("foo"); // returns FALSE // Validate integer from POST parameter - $_POST["key"]. // Works like intGet() Filter::intPost($key, $min_range = false, $max_range = false, $default = false); // Validate integer from COOKIES - $_COOKIE[$key] Filter::intCookie($key, $min_range = false, $max_range = false, $default = false); // and from SESSION Filter::intSession($key, $min_range = false, $max_range = false, $default = false); ?>
字符串
验证字符串值。您可以设置大小限制 - 最小和最大字符串长度。如果给定的字符串超出边界或不是字符串,则返回默认值。
<?php Filter::str($value, $minLength = 0, $maxLength = false, $default = false); // Examples: Filter::str("a"); // "a" Filter::str(1); // "1" Filter::str(" a "); // "a" Filter::str(""); // "" Filter::str("", 1); // false Filter::str(" a ", 1); // "a" Filter::str("ab", 1, 1); // false Filter::str("abc", 1, 2, "error"); // "error" Filter::str("abc", 1, false, "error"); // "abc" // Slightly different version of the Filter::str() method is Filter::plain() // This will firstly strip all tags and then it will act exactly like Filter::str() method. Filter::plain($value, $minLength = 0, $maxLength = false, $default = false); ?>
与整数过滤器类似,也有一些用于验证 $_GET、$_POST 和 $_COOKIE 数组中的字符串的过滤器。
<?php Filter::strGet($key, $minLength = 0, $maxLength = false, $default = false); Filter::strPost($key, $minLength = 0, $maxLength = false, $default = false); Filter::strCookie($key, $minLength = 0, $maxLength = false, $default = false); Filter::strSession($key, $minLength = 0, $maxLength = false, $default = false); // Validates plain text from $_GET, $_POST, $_COOKIE and $_SESSION parameters Filter::plainGet($key, $minLength = 0, $maxLength = false, $default = false); Filter::plainPost($key, $minLength = 0, $maxLength = false, $default = false); Filter::plainCookie($key, $minLength = 0, $maxLength = false, $default = false); Filter::plainSession($key, $minLength = 0, $maxLength = false, $default = false); ?>
URL
验证 URL,仅接受 http 或 https 协议。
<?php Filter::url($value, $default = false); // Examples: Filter::url("http://igrivi.com"); // true Filter::url("igrivi.com"); // false Filter::url("https://"); // false - The filter is mainly used for user inputs, so when we need URL, we intentionally don't want localhost Filter::url("8.8.8.8"); // false Filter::url("http://somedomain.com:81"); // true Filter::url("http://somedomain.com:6"); // false ?>
电子邮件
验证电子邮件地址。如果第三个参数设置为 true,它将检查邮件域的 MX 记录。如果电子邮件无效或 MX 记录不存在,则返回默认值。
<?php Filter::email($value, $default = false, $checkMxRecord = false); ?>
数组
<?php // Checks the existence of the key in a given array, returning default value if $key is not present. Filter::key($key, $array, $default = null); // Example: Filter::key("foo", array("one", "foo" => "bar", "foobar" => 2)); // "bar" // Validates $_GET[$key] value. Filter::get($key, $default = null); // Validates $_POST[$key] value. Filter::post($key, $default = null); // Validates $_COOKIE[$key] value. Filter::cookie($key, $default = null); // Validates $_SESSION[$key] value. Filter::session($key, $deafult = null); ?>