Programmer's Reference Guide

过滤器链

编写过滤器

Zend_Filter提供了一系列常用的过滤器,但是开发者经常需要为他们特殊的用例编写定制的过滤器。编写定制的过滤器很容易,只要实现Zend_Filter_Interface接口。

Zend_Filter_Interface接口定义了一个方法filter(),可被用户的类实现。使用Zend_Filter::addFilter()方法,可以把一个实现了这个接口的对象添加到过滤器链中。

下面的例子,示范了怎样编写一个定制的过滤器:

class MyFilter implements Zend_Filter_Interface
{
    public function filter($value)
    {
        // perform some transformation upon $value to arrive on $valueFiltered

        return $valueFiltered;
    }
}

       

添加上述过滤器的实例到过滤器链中:

$filterChain = new Zend_Filter();
$filterChain->addFilter(new MyFilter());

       


过滤器链

Comments

/**
* EmailMaskFilter
* A simple custom filter that will hide a part of the email
* letting user to see only the first letter and the domain
* the rest part uf the email is filled with wildcards
* ex: t***@gmail.com
*/
class EmailMaskFilter implements Zend_Filter_Interface
{
public function filter($value)
{
$mask = str_pad('', strpos($value, '@') - 1, '*');
$valueFiltered = substr_replace($value, $mask, 1, strlen($mask));
return $valueFiltered;
}
}
Where should I put my filter files to make ZF load them automatically?
This documentation is too pour!!!

The question of DoubleG is totally appropriate! How can you think to illustate how to write a custom filter without even tell WHERE to put it???

This is simple crazy.
autoloaderNamespaces[]= "CustomLibrary"

then you can call the filter something like
$usernameElement= new Zend_Form_Element_Text('username');
$usernameElement->addFilter(new CustomLibrary_Filter_MyFilter)
http://www.zendcasts.com/writing-custom-zend-filters-with-htmlpurifier/2011/06/

+ Add A Comment

Please do not report issues via comments; use the ZF Issue Tracker.

If you have a JIRA/Crowd account, we suggest you login first before commenting.

  • BBCode is allowed in the comment markup

  • Select a Version

    Languages Available

    Components

    Search the Manual