过滤,就是清除不需要的数据,留下想要的数据。
其调用方法如下,一:
$filter = new \Phalcon\Filter();
$filter->sanitize("some(one)@exa\mple.com", "email"); 得到的结果是:"someone@example.com"
另外一种方法是:
直接通过getPost/get获取
//gby(one)ftk\wf@qq.com
$email = $this->request->getPost("email", "email");
echo $email;
//gbyoneftkwf@qq.com
//$this->request->getPost("参数名", "email(需要验证的规则)");
自定义过滤器:
案一:
class IPv4Filter
{ public function filter($value)
{
return filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
} } $filter = new \Phalcon\Filter(); //Using an object
$filter->add('ipv4', new IPv4Filter()); //Sanitize with the "ipv4" filter
$filteredIp = $filter->sanitize("127.0.0.1", "ipv4"); 案二:
$filter = new \Phalcon\Filter(); //Using an anonymous function
$filter->add('md5', function($value) {
return preg_replace('/[^0-9a-f]/', '', $value);
}); //Sanitize with the "md5" filter
$filtered = $filter->sanitize($possibleMd5, "md5");
内置过滤器类型(Types of Built-in Filters)
The following are the built-in filters provided by this component:
Name | Description |
---|---|
string | Strip tags |
Remove all characters except letters, digits and !#$%&*+-/=?^_`{|}~@.[]. | |
int | Remove all characters except digits, plus and minus sign. |
float | Remove all characters except digits, dot, plus and minus sign. |
alphanum | Remove all characters except [a-zA-Z0-9] |
striptags | Applies the strip_tags function |
trim | Applies the trim function |
lower | Applies the strtolower function |
upper | Applies the strtoupper function |