在 Zend Form 元素上使用多个 PregReplace 过滤器
我希望能够在单个 Zend Form 元素上添加多个 PregReplace 过滤器。 我可以使用下面的代码添加一个 PregReplace 过滤器:
$word = new Zend_Form_Element_Text('word');
$word->addFilter('PregReplace', array(
'match' => '/bob/',
'replace' => 'john'
));
$this->addElement($word);
我已经尝试过
$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);
,但这仅意味着只有第二个过滤器起作用。
如何添加多个 PregReplace 过滤器?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您面临的问题是第二个过滤器将覆盖 Zend_Form_Element 中定义的过滤器堆栈 (
$this->_filters
) 中的第一个过滤器。正如大卫在问题评论中提到的,过滤器堆栈使用过滤器名称作为索引(
$this->_filters[$name] = $filter;
),这就是第二个过滤器覆盖第一个过滤器的原因一。为了解决这个问题,您可以使用自定义过滤器,如下所示:
这是使用内联函数()完成的,如果您没有使用 PHP 5.3 或更高版本,您可以设置您的回调如下以使其工作:
并在表单中的
init()
方法下添加:最后,如果您只想使用 PregReplace 过滤器,则与 Marcin 的答案不同(语法不正确), 做:
你仍然可以这样 应该可以解决问题;)
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:
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:
And add under your
init()
method in your form: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:
That should do the trick ;)
由于
PregReplace
使用 php 的 preg_replace 函数,我猜测这样的事情是可能的(preg_replace可以接受模式数组和相应的替换字符串数组):不过我还没有测试过。希望它能起作用。
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):I haven't tested it though. Hope it will work.
我无法让前面的示例与“PregReplace”一起使用。我改用 new Zend_Filter_PregReplace() 来调用它。现在它对我有用。
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.
我正在寻找相同的响应没有可用的版本
I was looking for same-response does not have a usable version