Chapter 20. Zend_Filter

Table of Contents

20.1. Introduction
20.1.1. What is a filter?
20.1.2. Basic usage of filters
20.1.3. Using the static get() method
20.2. Standard Filter Classes
20.2.1. Alnum
20.2.2. Alpha
20.2.3. BaseName
20.2.4. Decrypt
20.2.4.1. Decryption with Mcrypt
20.2.4.2. Decryption with OpenSSL
20.2.5. Digits
20.2.6. Dir
20.2.7. Encrypt
20.2.7.1. Encryption with Mcrypt
20.2.7.2. Encryption with OpenSSL
20.2.8. HtmlEntities
20.2.9. Int
20.2.10. StripNewlines
20.2.11. RealPath
20.2.12. StringToLower
20.2.13. StringToUpper
20.2.14. StringTrim
20.2.15. StripTags
20.3. Filter Chains
20.4. Writing Filters
20.5. Zend_Filter_Input
20.5.1. Declaring Filter and Validator Rules
20.5.2. Creating the Filter and Validator Processor
20.5.3. Retrieving Validated Fields and other Reports
20.5.3.1. Querying if the input is valid
20.5.3.2. Getting Invalid, Missing, or Unknown Fields
20.5.3.3. Getting Valid Fields
20.5.4. Using Metacommands to Control Filter or Validator Rules
20.5.4.1. The FIELDS metacommand
20.5.4.2. The PRESENCE metacommand
20.5.4.3. The DEFAULT_VALUE metacommand
20.5.4.4. The ALLOW_EMPTY metacommand
20.5.4.5. The BREAK_CHAIN metacommand
20.5.4.6. The MESSAGES metacommand
20.5.4.7. Using options to set metacommands for all rules
20.5.5. Adding Filter Class Namespaces
20.6. Zend_Filter_Inflector
20.6.1. Operation
20.6.2. Setting Paths To Alternate Filters
20.6.3. Setting the Inflector Target
20.6.4. Inflection Rules
20.6.4.1. Static Rules
20.6.4.2. Filter Inflector Rules
20.6.4.3. Setting Many Rules At Once
20.6.5. Utility Methods
20.6.6. Using Zend_Config with Zend_Filter_Inflector

20.1. Introduction

The Zend_Filter component provides a set of commonly needed data filters. It also provides a simple filter chaining mechanism by which multiple filters may be applied to a single datum in a user-defined order.

20.1.1. What is a filter?

In the physical world, a filter is typically used for removing unwanted portions of input, and the desired portion of the input passes through as filter output (e.g., coffee). In such scenarios, a filter is an operator that produces a subset of the input. This type of filtering is useful for web applications - removing illegal input, trimming unnecessary white space, etc.

This basic definition of a filter may be extended to include generalized transformations upon input. A common transformation applied in web applications is the escaping of HTML entities. For example, if a form field is automatically populated with untrusted input (e.g., from a web browser), this value should either be free of HTML entities or contain only escaped HTML entities, in order to prevent undesired behavior and security vulnerabilities. To meet this requirement, HTML entities that appear in the input must either be removed or escaped. Of course, which approach is more appropriate depends on the situation. A filter that removes the HTML entities operates within the scope of the first definition of filter - an operator that produces a subset of the input. A filter that escapes the HTML entities, however, transforms the input (e.g., "&" is transformed to "&"). Supporting such use cases for web developers is important, and "to filter," in the context of using Zend_Filter, means to perform some transformations upon input data.

20.1.2. Basic usage of filters

Having this filter definition established provides the foundation for Zend_Filter_Interface, which requires a single method named filter() to be implemented by a filter class.

Following is a basic example of using a filter upon two input data, the ampersand (&) and double quote (") characters:

$htmlEntities = new Zend_Filter_HtmlEntities();

echo $htmlEntities->filter('&'); // &
echo $htmlEntities->filter('"'); // "

            

20.1.3. Using the static get() method

If it is inconvenient to load a given filter class and create an instance of the filter, you can use the static method Zend_Filter::get() as an alternative invocation style. The first argument of this method is a data input value, that you would pass to the filter() method. The second argument is a string, which corresponds to the basename of the filter class, relative to the Zend_Filter namespace. The get() method automatically loads the class, creates an instance, and applies the filter() method to the data input.

echo Zend_Filter::get('&', 'HtmlEntities');

            

You can also pass an array of constructor arguments, if they are needed for the filter class.

echo Zend_Filter::get('"', 'HtmlEntities', array(ENT_QUOTES));

            

The static usage can be convenient for invoking a filter ad hoc, but if you have the need to run a filter for multiple inputs, it's more efficient to follow the first example above, creating an instance of the filter object and calling its filter() method.

Also, the Zend_Filter_Input class allows you to instantiate and run multiple filter and validator classes on demand to process sets of input data. See Section 20.5, “Zend_Filter_Input”.