過濾,就是清除不須要的數據,留下想要的數據。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 |