Chapter 11. Zend_Filter

Table of Contents

11.1. Zend_Filter
11.1.1. Introduction
11.1.2. Use Cases
11.2. Filter Chains
11.3. Writing Filters
11.4. Zend_Filter_Input
11.4.1. Introduction
11.4.2. Whitelist Filtering
11.4.3. Blind Filtering
11.4.4. Blacklist Filtering
11.4.5. Theory of Operation
11.4.6. Use Cases

11.1. Zend_Filter

11.1.1. Introduction

Zend_Filter provides a library of static methods for filtering data. For input filtering, you should use Section 11.4, “Zend_Filter_Input” instead, because it provides a framework for filtering input using the methods provided by this class. However, because Zend_Filter_Input is designed primarily for arrays, Zend_Filter can be useful for filtering scalars, because it behaves like PHP's string functions:

    <?php
    
    $alphaUsername = Zend_Filter::getAlpha('John123Doe');
    
    /* $alphaUsername = 'JohnDoe'; */
    
    ?>
        

11.1.2. Use Cases

In each of these use cases, $value represents an arbitrary scalar value.

Whitelist Filtering:

    <?php
    
    if (Zend_Filter::isEmail($value)) {
        /* $value is a valid email format. */
    } else {
        /* $value is not a valid email format. */
    }
    
    ?>
        

Blind Filtering:

    <?php
    
    $alphaName = Zend_Filter::getAlpha($value);
    
    ?>
        

Blacklist Filtering:

    <?php
    
    $taglessComment = Zend_Filter::noTags($value);
    
    ?>