【问题标题】:Using Zend_filter_input standalone独立使用 Zend_filter_input
【发布时间】:2009-08-26 07:16:05
【问题描述】:

是否可以将 Zend_Filter_Input 用作通用输入过滤器?我想用它来过滤所有表单字段(条形标签等,但没有验证)。所有示例似乎都包含一个 $validators 数组,并假设我会在输入的过程中知道字段的名称。

由于项目的性质、时间尺度等,不可能使用 Zend_Form 重写表单。有一个通用的 Form 类可以处理所有的表单输入,所以我需要在那里进行过滤。

谢谢!

卢克。

【问题讨论】:

    标签: php zend-framework


    【解决方案1】:

    您可以简单地为 $validators 参数传递一个空数组来跳过验证并简单地使用过滤。

    您是说您不知道要传递给Zend_Filter_Input 实例的字段名称吗?您可以使用通配符 *-field 将过滤器应用于所有输入字段。这是你要求的吗?

    $input = new Zend_Filter_Input(array(
        '*' => 'StripTags'
    ), array(), $data);
    

    将使用Zend_Filter_StripTags 过滤器过滤$data 中的所有值。

    编辑:

    使用

    检索值
    $escaped = $input->getEscaped(); // will be automatically run through an HTML-entities-filter
    // or
    $unescaped = $input->getUnescaped(); // the values as they come out of the filter-chain.
    

    【讨论】:

    • 谢谢,我试过了: public static function zfFilterInput($input) { $filters = array('*' => array('StringTrim','HtmlEntities','StripTags')); $input = new Zend_Filter_Input($filters,array(),$input);返回$输入; } 但得到一个错误,指出参数 3 不能是一个数组。我是否需要循环遍历数据数组以及如何将清理后的数据取出:?
    • 您在两次使用变量 $input 时遇到问题。首先你的函数参数被称为 $input 然后你将 Zend_Filter_Input 分配给 $input 使你的代码无用。
    • 为什么首先将所有内容编码为 HTML-entities,然后再进行 strip-tags?运行 HTML-entities 过滤器后没有任何标记。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多