开发者

Using multiple PregReplace filters on a Zend Form element

开发者 https://www.devze.com 2023-03-20 17:55 出处:网络
I want to be able to add multiple PregReplace filters on a single Zend Form element. I can add one PregReplace filter using th开发者_StackOverflow社区e code below:

I want to be able to add multiple PregReplace filters on a single Zend Form element. I can add one PregReplace filter using th开发者_StackOverflow社区e code below:

$word = new Zend_Form_Element_Text('word');
$word->addFilter('PregReplace', array(
        'match' => '/bob/', 
        'replace' => 'john'
    ));
$this->addElement($word);

I've tried

$word = new Zend_Form_Element_Text('word');
$word->addFilter('PregReplace', array(
        'match' => '/bob/', 
        'replace' => 'john'
    ));
$word->addFilter('PregReplace', array(
        'match' => '/sam/', 
        'replace' => 'dave'
    ));
$this->addElement($word);    

but this just meant only the second filter worked.

How do I add multiple PregReplace filters?


The problem you're facing is that the second filter will override the first one in the filters stack ($this->_filters) defined in Zend_Form_Element.

As David mentioned in the question comments, the filters stack use filter names as index ($this->_filters[$name] = $filter;) this is the reason why the second filter override the first one.

In order to resolve this problem, you can use a custom filter as follows:

$element->addFilter('callback', function($v) { return preg_replace(array('/bob/', '/sam/'),array('john', 'dave'), $v); });

This is done using an inline function(), in case you're not using PHP version 5.3 or higher, you can set your callback as follows to make it work:

$element->addFilter('callback', array('callback' => array($this, 'funcName')));

And add under your init() method in your form:

function funcName($v) {
    return preg_replace(array('/bob/', '/sam/'), array('john', 'dave'), $v);
}

At last, if you want to use only the PregReplace filter, unlike Marcin's answer (the syntax is incorrect), you can still do it this way:

$element->addFilter('pregReplace', array(
          array('match' => array('/bob/', '/sam/'),
                'replace' => array('john', 'dave')
)));

That should do the trick ;)


Since PregReplace uses php's preg_replace function, I guess something like this would be possible (preg_replace can accepts arrays of patterns and array of corresponding replacement strings):

$word = new Zend_Form_Element_Text('word');
$word->addFilter('PregReplace', array(
        'match'   => array('/bob/', '/sam/'), 
        'replace' => array('john' ,  dave)
    ));
$this->addElement($word);

I haven't tested it though. Hope it will work.


I was unable to get the previous example to work with 'PregReplace'. I switched instead to calling it with new Zend_Filter_PregReplace(). It now works for me.

$word->addFilter(new Zend_Filter_PregReplace(array(
                'match' => array('/bob/', '/sam/'), 
                'replace'=> array('john', 'dave'))
));


I was looking for same-response does not have a usable version

$word->addFilter(new Zend_Filter_PregReplace(new Zend_Config(array(
 'match'=>array('/bob/', '/sam/'),
 'replace'=>array('john', 'dave')
))));
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号