在 Zend Form 元素上使用多个 PregReplace 过滤器

发布于 2024-11-23 18:27:49 字数 699 浏览 4 评论 0原文

我希望能够在单个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我们只是彼此的过ke 2024-11-30 18:27:49

您面临的问题是第二个过滤器将覆盖 Zend_Form_Element 中定义的过滤器堆栈 ($this->_filters) 中的第一个过滤器。

正如大卫在问题评论中提到的,过滤器堆栈使用过滤器名称作为索引($this->_filters[$name] = $filter;),这就是第二个过滤器覆盖第一个过滤器的原因一。

为了解决这个问题,您可以使用自定义过滤器,如下所示:

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

这是使用内联函数()完成的,如果您没有使用 PHP 5.3 或更高版本,您可以设置您的回调如下以使其工作:

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

并在表单中的 init() 方法下添加:

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

最后,如果您只想使用 PregReplace 过滤器,则与 Marcin 的答案不同(语法不正确), 做:

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

你仍然可以这样 应该可以解决问题;)

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

淡墨 2024-11-30 18:27:49

由于 PregReplace 使用 php 的 preg_replace 函数,我猜测这样的事情是可能的(preg_replace可以接受模式数组和相应的替换字符串数组):

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

不过我还没有测试过。希望它能起作用。

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.

南城旧梦 2024-11-30 18:27:49

我无法让前面的示例与“PregReplace”一起使用。我改用 new Zend_Filter_PregReplace() 来调用它。现在它对我有用。

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

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'))
));
枕花眠 2024-11-30 18:27:49

我正在寻找相同的响应没有可用的版本

$word->addFilter(new Zend_Filter_PregReplace(new Zend_Config(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')
))));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文