Using multiple PregReplace filters on a Zend Form element
Asked Answered
R

4

9

I want to be able to add multiple PregReplace filters on a single Zend Form element. I can add one PregReplace filter using the 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?

Radmilla answered 15/7, 2011 at 10:6 Comment(1)
addFilter() uses the classname as an internal registry key, so apparently you can't have multiple filters of the same class. Kind of surprising that it doesn't allow an option to specify the key. Maybe worth filing as an issue.Jerrelljerri
C
6

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 ;)

Crean answered 3/4, 2012 at 18:13 Comment(6)
I did it and it shows this error : Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash . php version is PHP 5.3.1Elegance
My guess is that there is something wrong with your pattern. Could you post your code here? I just tried my code in example and it works perfectly.Crean
Good answer, IMO $element->addFilter('PregReplace', array(array('match' => array(), 'replace' => array()))); is the best way to go about this.Collectivity
this is my code $explain = new Zend_Form_Element_Textarea('explain'); $explain -> setLabel('explain') ->addFilter('callback', array('callback' => array($this, 'mypregReplace'))) ->setValue($data['explain']) ->addDecorator('HtmlTag', array('tag' => 'div'));Elegance
and this is my function in that class ` public function mypregReplace($v) { return preg_replace( array("%script/uploaded%" , "http://". $_SERVER['HTTP_HOST'] . '/public/script/uploaded'), array( "[\.\./]" , ""), $v); }` it show this error **Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in **Elegance
My guess was right, there is definitely something wrong with your pattern. Use a tool such as this one to write your pattern. I don't know exactly what you're trying to do, but I guess it's off-topic compared to your primary question.Crean
D
1

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.

Daves answered 15/7, 2011 at 11:16 Comment(1)
Thanks Marcin, unfortunately that didn't work. I received the error 'Zend_Filter_PregReplace does not have a valid MatchPattern set'Radmilla
S
0

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'))
));
Szabo answered 30/1, 2012 at 20:26 Comment(1)
i did the same work but it shows me this error : "Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash" when I run every filter separatley they work fine without error!!Elegance
H
-1

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')
))));
Haemic answered 13/8, 2013 at 9:4 Comment(1)
Please post annswer in English language only.Oscillation

© 2022 - 2024 — McMap. All rights reserved.