Programmer's Reference Guide

Filter Chains

Writing Filters

Zend_Filter supplies a set of commonly needed filters, but developers will often need to write custom filters for their particular use cases. The task of writing a custom filter is facilitated by implementing Zend_Filter_Interface.

Zend_Filter_Interface defines a single method, filter(), that may be implemented by user classes. An object that implements this interface may be added to a filter chain with Zend_Filter::addFilter().

The following example demonstrates how to write a custom filter:

  1. class MyFilter implements Zend_Filter_Interface
  2. {
  3.     public function filter($value)
  4.     {
  5.         // perform some transformation upon $value to arrive on $valueFiltered
  6.  
  7.         return $valueFiltered;
  8.     }
  9. }

To add an instance of the filter defined above to a filter chain:

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

Filter Chains

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