简体   繁体   English

用Zend中的破折号代替空格?

[英]replace space with dash in Zend?

    if ($settings->getSetting('que_tags', 0)) {
        $this->addElement('Text', 'qtags', array(
            'label' => Zend_Registry::get('Zend_Translate')->_('Tags'),
            'autocomplete' => 'on',
            'description' => Zend_Registry::get('Zend_Translate')->_('Use commas to separate tags.'),
            'allowEmpty' => false,
            'filters' => array('StringTrim', 'StringToLower', new Engine_Filter_Censor()),
        ));

I would like to change this: 我想更改此:

"United Kingdom" to "united-kingdom". “联合王国”改为“联合王国”。

I was able to insert StringToLower on the code but when I added SeparatorToDash , it didn't work. 我可以在代码上插入StringToLower ,但是当我添加SeparatorToDash ,它不起作用。

Is there a simple way to make the change happen by just adding/inserting a line to the code above? 是否有简单的方法可以通过仅在上面的代码中添加/插入一行来进行更改?

Zend delivers such filter out of the box, the class is called Zend_Filter_Word_SeparatorToDash : Zend提供了开箱即用的此类过滤器,该类称为Zend_Filter_Word_SeparatorToDash

$form = new Zend_Form_Element('test');
$form->addFilter(new \Zend_Filter_Word_SeparatorToDash());
$form->setValue('United Kingdom united kingdom');
var_dump($form->getValue()); // United-Kingdom-united-kingdom

More advanced filtering might require creating filter class from scratch or as an alternative you can always use Zend_Filter_Callback : 更高级的过滤可能需要从头开始创建过滤器类,或者您可以始终使用Zend_Filter_Callback作为替代:

$form = new Zend_Form_Element('test');
$form->addFilter(
    new \Zend_Filter_Callback(
        function ($value) {
            return str_replace(' ', '-', strtolower($value));
        }
    )
);
$form->setValue('United Kingdom united kingdom');
var_dump($form->getValue()); // united-kingdom-united-kingdom

For more filters can check zend filter folder, there is quite a lot of them. 如需更多过滤器,请查看zend过滤器文件夹,其中有很多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM