过滤,就是清除不须要的数据,留下想要的数据。php
其调用方法以下,一:html
$filter = new \Phalcon\Filter(); $filter->sanitize("some(one)@exa\mple.com", "email"); 获得的结果是:"someone@example.com"
另一种方法是:git
直接经过getPost/get获取ide
//gby(one)ftk\wf@qq.com $email = $this->request->getPost("email", "email"); echo $email; //gbyoneftkwf@qq.com //$this->request->getPost("参数名", "email(须要验证的规则)");
自定义过滤器:ui
案一: 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");
The following are the built-in filters provided by this component:this
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 |