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?

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. Commented Jul 16, 2011 at 1:05

4 Answers 4

6
+50

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

Sign up to request clarification or add additional context in comments.

6 Comments

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.1
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.
Good answer, IMO $element->addFilter('PregReplace', array(array('match' => array(), 'replace' => array()))); is the best way to go about this.
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'));
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 **
|
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.

1 Comment

Thanks Marcin, unfortunately that didn't work. I received the error 'Zend_Filter_PregReplace does not have a valid MatchPattern set'
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'))
));

1 Comment

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

1 Comment

Please post annswer in English language only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.