Standard Filter ClassesZend Framework comes with a standard set of filters, which are ready for you to use. AlnumReturns the string $value, removing all but alphabetic and digit characters. This filter includes an option to also allow white space characters.
AlphaReturns the string $value, removing all but alphabetic characters. This filter includes an option to also allow white space characters. BaseNameGiven a string containing a path to a file, this filter will return the base name of the file BooleanThis filter changes a given input to be a BOOLEAN value. This is often useful when working with databases or when processing form values. Default behaviour for Zend_Filter_BooleanBy default, this filter works by casting the input to a BOOLEAN value; in other words, it operates in a similar fashion to calling (boolean) $value.
This means that without providing any configuration, Zend_Filter_Boolean accepts all input types and returns a BOOLEAN just as you would get by type casting to BOOLEAN. Changing behaviour for Zend_Filter_BooleanSometimes casting with (boolean) will not suffice. Zend_Filter_Boolean allows you to configure specific types to convert, as well as which to omit. The following types can be handled:
All other given values will return TRUE by default. There are several ways to select which of the above types are filtered. You can give one or multiple types and add them, you can give an array, you can use constants, or you can give a textual string. See the following examples:
You can also give an instance of Zend_Config to set the desired types. To set types after instantiation, use the setType() method. Localized booleansAs mentioned previously, Zend_Filter_Boolean can also recognise localized "yes" and "no" strings. This means that you can ask your customer in a form for "yes" or "no" within his native language and Zend_Filter_Boolean will convert the response to the appropriate boolean value. To set the desired locale, you can either use the locale option, or the method setLocale(). Disable castingSometimes it is necessary to recognise only TRUE or FALSE and return all other values without changes. Zend_Filter_Boolean allows you to do this by setting the casting option to FALSE. In this case Zend_Filter_Boolean will work as described in the following table, which shows which values return TRUE or FALSE. All other given values are returned without change when casting is set to FALSE
The following example shows the behaviour when changing the casting option: CallbackThis filter allows you to use own methods in conjunction with Zend_Filter. You don't have to create a new filter when you already have a method which does the job. Let's expect we want to create a filter which reverses a string.
As you can see it's really simple to use a callback to define a own filter. It is also possible to use a method, which is defined within a class, by giving an array as callback. To get the actual set callback use getCallback() and to set another callback use setCallback(). It is also possible to define default parameters, which are given to the called method as array when the filter is executed. This array will be concatenated with the value which will be filtered. When you would call the above method definition manually it would look like this:
Compress and DecompressThese two filters are capable of compressing and decompressing strings, files, and directories. They make use of adapters and support the following compression formats:
Each compression format has different capabilities as described below. All compression filters may be used in approximately the same ways, and differ primarily in the options available and the type of compression they offer (both algorithmically as well as string vs. file vs. directory) Generic handlingTo create a compression filter you need to select the compression format you want to use. The following description takes the Bz2 adapter. Details for all other adapters are described after this section. The two filters are basically identical, in that they utilize the same backends. Zend_Filter_Compress should be used when you wish to compress items, and Zend_Filter_Decompress should be used when you wish to decompress items. For instance, if we want to compress a string, we have to initiate Zend_Filter_Compress and indicate the desired adapter.
To use a different adapter, you simply specify it to the constructor. You may also provide an array of options or Zend_Config object. If you do, provide minimally the key "adapter", and then either the key "options" or "adapterOptions" (which should be an array of options to provide to the adapter on instantiation).
Almost the same usage is we want to decompress a string. We just have to use the decompression filter in this case.
To get the compressed string, we have to give the original string. The filtered value is the compressed version of the original string.
Decompression works the same way.
Creating an archiveCreating an archive file works almost the same as compressing a string. However, in this case we need an additional parameter which holds the name of the archive we want to create. In the above example the uncompressed string is compressed, and is then written into the given archive file.
When you want to compress a file, then you must give the name of the file with its path. You may also specify a directory instead of a filename. In this case the whole directory with all its files and subdirectories will be compressed into the archive.
Decompressing an archiveDecompressing an archive file works almost like compressing it. You must specify either the archive parameter, or give the filename of the archive when you decompress the file.
Some adapters support decompressing the archive into another subdirectory. In this case you can set the target parameter.
Bz2 AdapterThe Bz2 Adapter can compress and decompress:
This adapter makes use of PHP's Bz2 extension. To customize compression, this adapter supports the following options:
All options can be set at instantiation or by using a related method. For example, the related methods for 'Blocksize' are getBlocksize() and setBlocksize(). You can also use the setOptions() method which accepts all options as array. Gz AdapterThe Gz Adapter can compress and decompress:
This adapter makes use of PHP's Zlib extension. To customize the compression this adapter supports the following options:
All options can be set at initiation or by using a related method. For example, the related methods for 'Level' are getLevel() and setLevel(). You can also use the setOptions() method which accepts all options as array. Lzf AdapterThe Lzf Adapter can compress and decompress:
This adapter makes use of PHP's Lzf extension. There are no options available to customize this adapter. Rar AdapterThe Rar Adapter can compress and decompress:
This adapter makes use of PHP's Rar extension.
To customize the compression this adapter supports the following options:
All options can be set at instantiation or by using a related method. For example, the related methods for 'Target' are getTarget() and setTarget(). You can also use the setOptions() method which accepts all options as array. Tar AdapterThe Tar Adapter can compress and decompress:
This adapter makes use of PEAR's Archive_Tar component. To customize the compression this adapter supports the following options:
All options can be set at instantiation or by using a related method. For example, the related methods for 'Target' are getTarget() and setTarget(). You can also use the setOptions() method which accepts all options as array.
Zip AdapterThe Zip Adapter can compress and decompress:
This adapter makes use of PHP's Zip extension. To customize the compression this adapter supports the following options:
All options can be set at instantiation or by using a related method. For example, the related methods for 'Target' are getTarget() and setTarget(). You can also use the setOptions() method which accepts all options as array. DecryptThis filter will decrypt any given string with the provided setting. Therefor it makes use of Adapters. Actually there are adapters for the Mcrypt and OpenSSL extensions from PHP. For details about how to encrypt content look at the Encrypt filter. As the basics are covered within the Encrypt filter, we will describe here only the needed additional methods and changes for decryption. Decryption with McryptFor decrypting content which was previously encrypted with Mcrypt you need to have the options with which the encryption has been called. There is one eminent difference for you. When you did not provide a vector at encryption you need to get it after you encrypted the content by using the getVector() method on the encryption filter. Without the correct vector you will not be able to decrypt the content. As soon as you have provided all options decryption is as simple as encryption.
Decryption with OpenSSLDecryption with OpenSSL is as simple as encryption. But you need to have all data from the person who encrypted the content. For decryption with OpenSSL you need:
Optionally it could be necessary to provide the passphrase for decrypting the keys themself by using the setPassphrase() method.
At last, decode the content. Our complete example for decrypting the previously encrypted content looks like this.
DigitsReturns the string $value, removing all but digit characters. DirReturns directory name component of path. EncryptThis filter will encrypt any given string with the provided setting. Therefor it makes use of Adapters. Actually there are adapters for the Mcrypt and OpenSSL extensions from PHP. As these two encryption methodologies work completely different, also the usage of the adapters differ. You have to select the adapter you want to use when initiating the filter. To set another adapter you can also use setAdapter(), and the getAdapter() method to receive the actual set adapter.
Encryption with McryptWhen you have installed the Mcrypt extension you can use the Mcrypt adapter. This adapter supports the following options at initiation:
If you give a string instead of an array, this string will be used as key. You can get and set the encryption values also afterwards with the getEncryption() and setEncryption() methods.
You can get or set the encryption vector by calling getVector() and setVector(). A given string will be truncated or padded to the needed vector size of the used algorithm.
Encryption with OpenSSLWhen you have installed the OpenSSL extension you can use the OpenSSL adapter. This adapter supports the following options at initiation:
You can get or set the public keys also afterwards with the getPublicKey() and setPublicKey() methods. The private key can also be get and set with the related getPrivateKey() and setPrivateKey() methods.
When you want to encode also the keys, then you have to provide a passphrase with the setPassphrase() method. When you want to decode content which was encoded with a passphrase you will not only need the public key, but also the passphrase to decode the encrypted key.
At last, when you use OpenSSL you need to give the receiver the encrypted content, the passphrase when have provided one, and the envelope keys for decryption. This means for you, that you have to get the envelope keys after the encryption with the getEnvelopeKey() method. So our complete example for encrypting content with OpenSSL look like this.
Simplified usage with OpensslAs seen before, you need to get the envelope key to be able to decrypt the previous encrypted value. This can be very annoying when you work with multiple values. To have a simplified usage you can set the package option to TRUE. The default value is FALSE.
Now the returned value contains the encrypted value and the envelope. You don't need to get them after the compression. But, and this is the negative aspect of this feature, the encrypted value can now only be decrypted by using Zend_Filter_Encrypt. Compressing the contentBased on the original value, the encrypted value can be a very large string. To reduce the value Zend_Filter_Encrypt allows the usage of compression. The compression option can eighter be set to the name of a compression adapter, or to an array which sets all wished options for the compression adapter.
HtmlEntitiesReturns the string $value, converting characters to their corresponding HTML entity equivalents where they exist. IntReturns (int) $value LocalizedToNormalizedThis filter will change any given localized input to it's normalized representation. It uses in Background Zend_Locale to do this transformation for you. This allows your user to enter informations in his own language notation, and you can then store the normalized value into your database for example.
The following input types can be normalized:
Any other input will be returned as it, without changing it.
Normalization for numbersAny given number like integer, float or real value, can be normalized. Note, that numbers in scientific notation, can actually not be handled by this filter. So how does this normalization work in detail for numbers:
Let's expect you have set the locale 'de' as application wide locale. Zend_Filter_LocalizedToNormalized will take the set locale and use it to detect which sort of input you gave. In our example it was a value with precision. Now the filter will return you the normalized representation for this value as string. You can also control how your normalized number has to look like. Therefor you can give all options which are also used by Zend_Locale_Format. The most common are:
For details about how these options are used take a look into this Zend_Locale chapter. Below is a example with defined precision so you can see how options work:
Normalization for date and timeInput for date and time values can also be normalized. All given date and time values will be returned as array, where each date part is given within a own key.
Let's expect you have set the locale 'de' again. Now the input is automatically detected as date, and you will get a named array in return. Of course you can also control how your date input looks like with the date_format and the locale option.
NormalizedToLocalizedThis filter is the reverse of the filter Zend_Filter_LocalizedToNormalized and will change any given normalized input to it's localized representation. It uses in Background Zend_Locale to do this transformation for you. This allows you to give your user any stored normalised value in a localized manner, your user is more common to.
The following input types can be localized:
Any other input will be returned as it, without changing it. Localization for numbersAny given number like integer, float or real value, can be localized. Note, that numbers in scientific notation, can actually not be handled by this filter. So how does localization work in detail for numbers:
Let's expect you have set the locale 'de' as application wide locale. Zend_Filter_NormalizedToLocalized will take the set locale and use it to detect which sort of output you want to have. In our example it was a value with precision. Now the filter will return you the localized representation for this value as string. You can also control how your localized number has to look like. Therefor you can give all options which are also used by Zend_Locale_Format. The most common are:
For details about how these options are used take a look into this Zend_Locale chapter. Below is a example with defined precision so you can see how options work:
Localization for date and timeNormalized for date and time values can also be localized. All given date and time values will be returned as string, with the format defined by the set locale.
Let's expect you have set the locale 'de' again. Now the input is automatically detected as date, and will be returned in the format defined by the locale 'de'. Of course you can also control how your date input looks like with the date_format, and the locale option. NullThis filter will change the given input to be NULL if it meets specific criteria. This is often necessary when you work with databases and want to have a NULL value instead of a boolean or any other type. Default behaviour for Zend_Filter_NullPer default this filter works like PHP's empty() method; in other words, if empty() returns a boolean TRUE, then a NULL value will be returned.
This means that without providing any configuration, Zend_Filter_Null will accept all input types and return NULL in the same cases as empty(). Any other value will be returned as is, without any changes. Changing behaviour for Zend_Filter_NullSometimes it's not enough to filter based on empty(). Therefor Zend_Filter_Null allows you to configure which type will be converted and which not. The following types can be handled:
There are several ways to select which of the above types are filtered. You can give one or multiple types and add them, you can give an array, you can use constants, or you can give a textual string. See the following examples:
You can also give an instance of Zend_Config to set the wished types. To set types afterwards use setType(). PregReplaceZend_Filter_PregReplace performs a search using regular expressions and replaces all found elements. The option match has to be given to set the pattern which will be searched for. It can be a string for a single pattern, or an array of strings for multiple pattern. To set the pattern which will be used as replacement the option replace has to be used. It can be a string for a single pattern, or an array of strings for multiple pattern.
You can use getMatchPattern() and setMatchPattern() to set the matching pattern afterwards. To set the replacement pattern you can use getReplacement() and setReplacement(). For a more complex usage take a look into PHP's » PCRE Pattern Chapter. RealPathThis filter will resolve given links and pathnames and returns canonicalized absolute pathnames. References to '/./', '/../' and extra '/' characters in the input path will be stripped. The resulting path will not have any symbolic link, '/./' or '/../' character. Zend_Filter_RealPath will return FALSE on failure, e.g. if the file does not exist. On BSD systems Zend_Filter_RealPath doesn't fail if only the last path component doesn't exist, while other systems will return FALSE.
Sometimes it is useful to get also paths when they don't exist, f.e. when you want to get the real path for a path which you want to create. You can then either give a FALSE at initiation, or use setExists() to set it.
StringToLowerThis filter converts any input to be lowercased.
Per default it will only handle characters from the actual locale of your server. Characters from other charsets would be ignored. Still, it's possible to also lowercase them when the mbstring extension is available in your environment. Simply set the wished encoding when initiating the StringToLower filter. Or use the setEncoding() method to change the encoding afterwards.
StringToUpperThis filter converts any input to be uppercased.
Like the StringToLower filter, this filter handles only characters from the actual locale of your server. Using different character sets works the same as with StringToLower.
StringTrimReturns the string $value with characters stripped from the beginning and end. StripNewlinesReturns the string $value without any newline control characters. StripTagsThis filter returns the input string, with all HTML and PHP tags stripped from it, except those that have been explicitly allowed. In addition to the ability to specify which tags are allowed, developers can specify which attributes are allowed across all allowed tags and for specific tags only.
|