Programmer's Reference Guide

简介

标准过滤器类

Zend Framework 带有一组标准的过滤器。

Alnum

返回只保留字母和数字的字符串 $value,这个过滤器包括一个允许空白字符的选项。

Alpha

返回只保留字母的字符串 $value,这个过滤器包括一个允许空白字符的选项。

BaseName

给定包含一个文件的路径字符串,这个过路器将返回这个文件的基本名(base name)。

Digits

返回只保留数字的字符串 $value

Dir

返回路径的名字部分。

HtmlEntities

返回转换成它们对应 HTML 实体的字符串 $value

Int

返回整数 $value

StripNewlines

返回不带任何新行控制符的字符串 $value

RealPath

扩展所有符号的链接和解析指向在输入路径里的 '/./', '/../' 和额外的 '/' 字符并且返回规范化后的绝对路径名,返回的结果路径将没有符号链接 '/./' 或 '/../' 部分。

如果失败,例如文件不存在,Zend_Filter_RealPath 将返回 FALSE。在 BSD 系统,如果只有路径最后部分不存在, Zend_Filter_RealPath 不会失败,但其它系统返回 FALSE

StringToLower

返回按需转换字母成小写的字符串 $value

StringToUpper

返回按需转换字母成大写的字符串 $value

StringTrim

返回从头到尾整理过的字符串 $value

StripTags

返回把 HTML 和 PHP 标签已剥离的(声明为允许的标签不会剥离)输入的字符串。除了能指定允许哪个标签,开发者也可以在所有允许的标签(或只对特定的标签)中指定哪个属性被允许。最后,这个过滤器提供控制是否注释(如 <!-- ... -->)被删除或允许。


简介

Comments

Zend_Filter_StripTags example when used with Zend_Form:


$form = new Zend_Form();
$form->addElement('text', 'name', array(
    'filters' => array(
        array('StripTags', array(array(
            'allowTags' => array('p', 'br', 'strong'),
            'allowAttribs' => array('style', 'title')
        )))
    )
));
Zend_Filter_StripTags example when used with Zend_Form:


$form = new Zend_Form();
$form->addElement('text', 'name', array(
    'filters' => array(
        array('StripTags', array(array(
            'allowTags' => array('p', 'br', 'strong'),
            'allowAttribs' => array('style', 'title')
        )))
    )
));
Zend_Filter_StripTags example when used with Zend_Form:


$form = new Zend_Form();
$form->addElement('text', 'name', array(
    'filters' => array(
        array('StripTags', array(array(
            'allowTags' => array('p', 'br', 'strong'),
            'allowAttribs' => array('style', 'title')
        )))
    )
));
Zend_Filter_StripTags example when used with Zend_Form:


$form = new Zend_Form();
$form->addElement('text', 'name', array(
    'filters' => array(
        array('StripTags', array(array(
            'allowTags' => array('p', 'br', 'strong'),
            'allowAttribs' => array('style', 'title')
        )))
    )
));
Using PregReplace filter. Example of filtering (not validating) a username from $_POST, stripping everything except digits, letters and underscore:

$fl = new Zend_Filter_PregReplace('/([^0-9a-z_])*/i', '');
$username = $fl->filter($this->_request->getPost('username', ''));
With a form, since the version 1.10.3, you need to do this :

$form = new Zend_Form();
$form->addElement('text', 'name', array(
'filters' => array(
array('StripTags', array('options' => array(
'allowTags' => array('p', 'br', 'strong'),
'allowAttribs' => array('style', 'title')
)))
)
));
This is weird..

My Zend 1.10.8 does not work as described above. For instance in the example:

$filter = new Zend_Filter_Int();
print $filter->filter('-4 is less than 0');
This will return '-4'.

...in my case it will return 0. There's not even any code in zend/filter/int.php that would remove non numeric values in my installation.

Also, I am somehow able to accomplish a situation where Zend_Filter_Boolean(); always returns true no matter what I try to do with it.

Coud it be that something's not loading properly, or I have some setting missing or I have failed to install Zend correctly?
this documentation sucks balls. Every header is the same color and size, so I cannot delineate between what sections are actually the list of Filters, and what sections are components & options for each filter. It's like reading a term paper with no fucking punctuation or paragraphs; it's one huge run-on sentence.

this documentation sucks balls. Every header is the same color and size, so I cannot delineate between what sections are actually the list of Filters, and what sections are components & options for each filter. It's like reading a term paper with no fucking punctuation or paragraphs; it's one huge run-on sentence.

Zend_Filter_StripNewLines <-> Zend_Filter_StripNewlines
Zend_Filter_Compress does not seem to handle file compression. The documantation states that you can specify a filename to compress in the filter method, however it only creates a compressed file of the fiilename!!!

To compress a file use the following:

$filter = new Zend_Filter_Compress(array(
'adapter' => 'Bz2',
'options' => array(
'archive' => 'filename.bz2'
),
));

$compressed = $filter->filter(file_get_contents(compressme.txt));
You cannot add two PregReplace filter. Only the last one will be used. Is this a bug ?
I am writing an XML file using DOMDocument. Expression "&lt;br&gt;" (without quote) is used for line break in XML. and also it's working well with all my XML files when parsed.

As when I pass this expression with text in a text node through php string e.g. $string = $textvalue."&lt;br&gt;";

and then passing $string to attribute text like

$root->appendChild($xmlDoc->createAttribute("caption"))->appendChild($xmlDoc->createTextNode($string ));

But, brouhaha... this generates textnode like

<root caption="&amp;lt;BR&amp;gt;">
</root>

This means some extra characters "amp;" are also added. So the parsed XML won't give a line break as &amp;lt;BR&amp;gt; will be recognized as plain text <br> and prints the same i.e. <br> as text.
Zend_Filter_Archive doesn't support creating a zip from a string

The source code shows this:
$zip = new ZipArchive();
$res = $zip->open($this->getArchive(), ZipArchive::CREATE | ZipArchive::OVERWRITE);
"Zend_Filter_Int allows you to transform a sclar value which contains into an integer." This sentence does not make sense at all.

Can someone fix it?
typo : "anglaise internationnale" => "anglaise internationale"
As raised (and closed as wontfix) here, there's an issue, apparently between the Autoloader and Zend_Filter_Decompress.

My workaround for this is to use the full ZF1 namespaced name for the adapter, like so:


$filter = new Zend_Filter_Decompress(array(
                        'adapter' => 'Zend_Filter_Compress_Zip',
                        'options' => array(
                            'target' => $target_directory,
                            )
                        ));

+ Add A Comment

Please do not report issues via comments; use the ZF Issue Tracker.

If you have a JIRA/Crowd account, we suggest you login first before commenting.

  • BBCode is allowed in the comment markup

  • Select a Version

    Languages Available

    Components

    Search the Manual