ZF-1213: Zend Filter Lacks Customizeable Regex Filter

Description

Due to its current limited nature, the existing filterset does not allow developers to create regex based filters that fulfill the on-the-fly creation of filters that implement the Filter Interface... to satisfy that, i propose Zend_Filter_Regex


<?php

/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Filter
 * @copyright  Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id:$
 */


/**
 * @see Zend_Filter_Interface
 */
require_once 'Zend/Filter/Interface.php';


/**
 * @category   Zend
 * @package    Zend_Filter
 * @copyright  Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Zend_Filter_Regex implements Zend_Filter_Interface
{
    
    protected $_matchRegex = null;
    protected $_replacement = '';
    
    /**
     * __construct()
     *
     * @param string $matchRegex
     * @param string $replacement
     */
    public function __construct($matchRegex, $replacement = '')
    {
        $this->_matchRegex  = $matchRegex;
        $this->_replacement = $replacement;
    }
    
    /**
     * Defined by Zend_Filter_Interface
     *
     * Returns the string $value, replacing matching characters with $replace
     *
     * @param  string $value
     * @return string
     */
    public function filter($value)
    {
        return preg_replace($this->_matchRegex, $this->_replacement, (string) $value);
    }
}




Comments

Reassigning to [~ralph]. Since this is a new filter, we retain backward compatibility and can add this along with appropriate unit tests and documentation. As an aside, [~andries] is presently working on manual documentation for the individual filters to supplement the API documentation.

Now available in the incubator:

http://framework.zend.com/fisheye/browse/…

Was added to core in r7037 under the name PregReplace.php

-ralph