码迷,mamicode.com
首页 > 其他好文 > 详细

phalcon: 过滤(Phalcon\Filter())

时间:2016-09-27 22:57:52      阅读:1050      评论:0      收藏:0      [点我收藏+]

标签:

过滤,就是清除不需要的数据,留下想要的数据。

其调用方法如下,一:

$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:

NameDescription
string Strip tags
email 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

 

 

phalcon: 过滤(Phalcon\Filter())

标签:

原文地址:http://www.cnblogs.com/achengmu/p/5914455.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!