22.2. Standard Filter Classes

Zend Framework comes with a standard set of filters, which are ready for you to use.

22.2.1. Alnum

Returns the string $value, removing all but alphabetic and digit characters. This filter includes an option to also allow white space characters.

[Note] Note

The alphabetic characters mean characters that makes up words in each language. However, the english alphabet is treated as the alphabetic characters in following languages: Chinese, Japanese, Korean. The language is specified by Zend_Locale.

22.2.2. Alpha

Returns the string $value, removing all but alphabetic characters. This filter includes an option to also allow white space characters.

22.2.3. BaseName

Given a string containing a path to a file, this filter will return the base name of the file

22.2.4. Callback

This 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.

$filter = new Zend_Filter_Callback('strrev');

print $filter->filter('Hello!');
// returns "!olleH"

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.

// Our classdefinition
class MyClass
{
    public function Reverse($param);
}

// The filter definition
$filter = new Zend_Filter_Callback(array('MyClass', 'Reverse'));
print $filter->filter('Hello!');

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.

$filter = new Zend_Filter_Callback(
    array(
        'callback' => 'MyMethod',
        'options'  => array('key' => 'param1', 'key2' => 'param2')
    )
);
$filter->filter(array('value' => 'Hello'));

When you would call the above method definition manually it would look like this:

$value = MyMethod('Hello', 'param1', 'param2');
[Note] Note

You should note that defining a callback method which can not be called will raise an exception.

22.2.5. Compress and Decompress

These two filters are capable of compressing and decompressing strings, files, and directories. They make use of adapters and support the following compression formats:

  • Bz2

  • Gz

  • Lzf

  • Rar

  • Tar

  • Zip

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)

22.2.5.1. Generic handling

To 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.

$filter = new Zend_Filter_Compress('Bz2');

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

$filter = new Zend_Filter_Compress(array(
    'adapter' => 'Bz2',
    'options' => array(
        'blocksize' => 8,
    ),
));
[Note] Default compression Adapter

When no compression adapter is given, then the Gz adapter will be used.

Almost the same usage is we want to decompress a string. We just have to use the decompression filter in this case.

$filter = new Zend_Filter_Decompress('Bz2');

To get the compressed string, we have to give the original string. The filtered value is the compressed version of the original string.

$filter     = new Zend_Filter_Compress('Bz2');
$compressed = $filter->filter('Uncompressed string');
// Returns the compressed string

Decompression works the same way.

$filter     = new Zend_Filter_Decompress('Bz2');
$compressed = $filter->filter('Compressed string');
// Returns the uncompressed string
[Note] Note on string compression

Not all adapters support string compression. Compression formats like Rar can only handle files and directories. For details, consult the section for the adapter you wish to use.

22.2.5.2. Creating an archive

Creating 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.

$filter     = new Zend_Filter_Compress(array(
    'adapter' => 'Bz2', 
    'options' => array(
        'archive' => 'filename.bz2',
    ),
));
$compressed = $filter->filter('Uncompressed string');
// Returns true on success and creates the archive file

In the above example the uncompressed string is compressed, and is then written into the given archive file.

[Note] Existing archives will be overwritten

The content of any existing file will be overwritten when the given filename of the archive already exists.

When you want to compress a file, then you must give the name of the file with its path.

$filter     = new Zend_Filter_Compress(array(
    'adapter' => 'Bz2', 
    'options' => array(
        'archive' => 'filename.bz2'
    ),
));
$compressed = $filter->filter('C:\temp\compressme.txt');
// Returns true on success and creates the archive file

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.

$filter     = new Zend_Filter_Compress(array(
    'adapter' => 'Bz2', 
    'options' => array(
        'archive' => 'filename.bz2'
    ),
));
$compressed = $filter->filter('C:\temp\somedir');
// Returns true on success and creates the archive file
[Note] Do not compress large or base directories

You should never compress large or base directories like a complete partition. Compressing a complete partition is a very time consuming task which can lead to massive problems on your server when there is not enough space or your script takes too much time.

22.2.5.3. Decompressing an archive

Decompressing 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.

$filter     = new Zend_Filter_Decompress('Bz2');
$compressed = $filter->filter('filename.bz2');
// Returns true on success and decompresses the archive file

Some adapters support decompressing the archive into another subdirectory. In this case you can set the target parameter.

$filter     = new Zend_Filter_Decompress(array(
    'adapter' => 'Zip', 
    'options' => array(
        'target' => 'C:\temp',
    )
));
$compressed = $filter->filter('filename.zip');
// Returns true on success and decompresses the archive file
// into the given target directory
[Note] Directories to extract to must exist

When you want to decompress an archive into a directory, then that directory must exist.

22.2.5.4. Bz2 Adapter

The Bz2 Adapter can compress and decompress:

  • Strings

  • Files

  • Directories

This adapter makes use of PHP's Bz2 extension.

To customize compression, this adapter supports the following options:

  • Archive: This parameter sets the archive file which should be used or created.

  • Blocksize: This parameter sets the blocksize to use. It can be from '0' to '9'. The default value is '4'.

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.

22.2.5.5. Gz Adapter

The Gz Adapter can compress and decompress:

  • Strings

  • Files

  • Directories

This adapter makes use of PHP's Zlib extension.

To customize the compression this adapter supports the following options:

  • Archive: This parameter sets the archive file which should be used or created.

  • Level: This compression level to use. It can be from '0' to '9'. The default value is '9'.

  • Mode: There are two supported modes. 'compress' and 'deflate'. The default value is 'compress'.

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.

22.2.5.6. Lzf Adapter

The Lzf Adapter can compress and decompress:

  • Strings

[Note] Lzf supports only strings

The Lzf adapter can not handle files and directories.

This adapter makes use of PHP's Lzf extension.

There are no options available to customize this adapter.

22.2.5.7. Rar Adapter

The Rar Adapter can compress and decompress:

  • Files

  • Directories

[Note] Rar does not support strings

The Rar Adapter can not handle strings.

This adapter makes use of PHP's Rar extension.

[Note] Rar compression not supported

Due to restrictions with the Rar compression format, there is no compression available for free. When you want to compress files into a new Rar archive, you must provide a callback to the adapter that can invoke a Rar compression program.

To customize the compression this adapter supports the following options:

  • Archive: This parameter sets the archive file which should be used or created.

  • Callback: A callback which provides compression support to this adapter.

  • Password: The password which has to be used for decompression.

  • Target: The target where the decompressed files will be written to.

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.

22.2.5.8. Tar Adapter

The Tar Adapter can compress and decompress:

  • Files

  • Directories

[Note] Tar does not support strings

The Tar Adapter can not handle strings.

This adapter makes use of PEAR's Archive_Tar component.

To customize the compression this adapter supports the following options:

  • Archive: This parameter sets the archive file which should be used or created.

  • Mode: A mode to use for compression. Supported are either 'null' which means no compression at all, 'Gz' which makes use of PHP's Zlib extension and 'Bz2' which makes use of PHP's Bz2 extension. The default value is 'null'.

  • Target: The target where the decompressed files will be written to.

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.

[Note] Directory usage

When compressing directories with Tar then the complete file path is used. This means that created Tar files will not only have the subdirectory but the complete path for the compressed file.

22.2.5.9. Zip Adapter

The Zip Adapter can compress and decompress:

  • Strings

  • Files

  • Directories

[Note] Zip does not support string decompression

The Zip Adapter can not handle decompression to a string; decompression will always be written to a file.

This adapter makes use of PHP's Zip extension.

To customize the compression this adapter supports the following options:

  • Archive: This parameter sets the archive file which should be used or created.

  • Target: The target where the decompressed files will be written to.

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.

22.2.6. Decrypt

This 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.

22.2.6.1. Decryption with Mcrypt

For decrypting content which was previously encrypted with Mcrypt you need to have the options with which the encryption has been called.

There is one emminent 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.

// Use the default blowfish settings
$filter = new Zend_Filter_Decrypt('myencryptionkey');

// Set the vector with which the content was encrypted
$filter->setVector('myvector');

$decrypted = $filter->filter('encoded_text_normally_unreadable');
print $decrypted;
[Note] Note

Note that you will get an exception if the mcrypt extension is not available in your environment.

[Note] Note

You should also note that all settings which be checked when you create the instance or when you call setEncryption(). If mcrypt detects problem with your settings an exception will be thrown.

22.2.6.2. Decryption with OpenSSL

Decryption 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:

  • private: Your private key which will be used for decrypting the content. The private key can be eighter a filename with path of the key file, or just the content of the key file itself.

  • envelope: The encrypted envelope key from the user who encrypted the content. You can eigther provide the path and filename of the key file, or just the content of the key file itself.

// Use openssl and provide a private key
$filter = new Zend_Filter_Decrypt(array(
    'adapter' => 'openssl',
    'private' => '/path/to/mykey/private.pem'
));

// of course you can also give the envelope keys at initiation
$filter->setEnvelopeKey(array(
    '/key/from/encoder/first.pem',
    '/key/from/encoder/second.pem'
));
[Note] Note

Note that the OpenSSL adapter will not work when you do not provide valid keys.

Optionally it could be necessary to provide the passphrase for decrypting the keys themself by using the setPassphrase() method.

// Use openssl and provide a private key
$filter = new Zend_Filter_Decrypt(array(
    'adapter' => 'openssl',
    'private' => '/path/to/mykey/private.pem'
));

// of course you can also give the envelope keys at initiation
$filter->setEnvelopeKey(array(
    '/key/from/encoder/first.pem',
    '/key/from/encoder/second.pem'
));
$filter->setPassphrase('mypassphrase');

At last, decode the content. Our complete example for decrypting the previously encrypted content looks like this.

// Use openssl and provide a private key
$filter = new Zend_Filter_Decrypt(array(
    'adapter' => 'openssl',
    'private' => '/path/to/mykey/private.pem'
));

// of course you can also give the envelope keys at initiation
$filter->setEnvelopeKey(array(
    '/key/from/encoder/first.pem',
    '/key/from/encoder/second.pem'
));
$filter->setPassphrase('mypassphrase');

$decrypted = $filter->filter('encoded_text_normally_unreadable');
print $decrypted;

22.2.7. Digits

Returns the string $value, removing all but digit characters.

22.2.8. Dir

Returns directory name component of path.

22.2.9. Encrypt

This 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.

// Use the Mcrypt adapter
$filter1 = new Zend_Filter_Encrypt(array('adapter' => 'mcrypt'));

// Use the OpenSSL adapter
$filter2 = new Zend_Filter_Encrypt(array('adapter' => 'openssl'));

To set another adapter you can also use setAdapter(), and the getAdapter() method to receive the actual set adapter.

// Use the Mcrypt adapter
$filter = new Zend_Filter_Encrypt();
$filter->setAdapter('openssl');
[Note] Note

When you do not supply the adapter option or do not use setAdapter, then the Mcrypt adapter will be used per default.

22.2.9.1. Encryption with Mcrypt

When you have installed the Mcrypt extension you can use the Mcrypt adapter. This adapter supports the following options at initiation:

  • key: The encryption key with which the input will be encrypted. You need the same key for decryption.

  • algorithm: The algorithm which has to be used. It should be one of the algorithm ciphers which can be found under PHP's mcrypt ciphers. If not set it defaults to blowfish.

  • algorithm_directory: The directory where the algorithm can be found. If not set it defaults to the path set within the mcrypt extension.

  • mode: The encryption mode which has to be used. It should be one of the modes which can be found under PHP's mcrypt modes. If not set it defaults to cbc.

  • mode_directory: The directory where the mode can be found. If not set it defaults to the path set within the mcrypt extension.

  • vector: The initialization vector which shall be used. If not set it will be a random vector.

  • salt: If the key should be used as salt value. The key used for encryption will then itself also be encrypted. Default is false.

If you give a string instead of an array, this string will be used as key.

You can get/set the encryption values also afterwards with the getEncryption() and setEncryption() methods.

[Note] Note

Note that you will get an exception if the mcrypt extension is not available in your environment.

[Note] Note

You should also note that all settings which be checked when you create the instance or when you call setEncryption(). If mcrypt detects problem with your settings an exception will be thrown.

You can get/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.

[Note] Note

Note that when you are not using an own vector, you must get the vector and store it. Otherwise you will not be able to decode the encoded string.

// Use the default blowfish settings
$filter = new Zend_Filter_Encrypt('myencryptionkey');

// Set a own vector, otherwise you must call getVector()
// and store this vector for later decryption
$filter->setVector('myvector');
// $filter->getVector();

$encrypted = $filter->filter('text_to_be_encoded');
print $encrypted;

// For decryption look at the Decrypt filter

22.2.9.2. Encryption with OpenSSL

When you have installed the OpenSSL extension you can use the OpenSSL adapter. This adapter supports the following options at initiation:

  • public: The public key of the user whom you want to provide the encrpted content. You can give multiple public keys by using an array. You can eigther provide the path and filename of the key file, or just the content of the key file itself.

  • private: Your private key which will be used for encrypting the content. Also the private key can be eighter a filename with path of the key file, or just the content of the key file itself.

You can get/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.

// Use openssl and provide a private key
$filter = new Zend_Filter_Encrypt(array(
    'adapter' => 'openssl',
    'private' => '/path/to/mykey/private.pem'
));

// of course you can also give the public keys at initiation
$filter->setPublicKey(array(
    '/public/key/path/first.pem',
    '/public/key/path/second.pem'
));
[Note] Note

Note that the OpenSSL adapter will not work when you do not provide valid keys.

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.

// Use openssl and provide a private key
$filter = new Zend_Filter_Encrypt(array(
    'adapter' => 'openssl',
    'private' => '/path/to/mykey/private.pem'
));

// of course you can also give the public keys at initiation
$filter->setPublicKey(array(
    '/public/key/path/first.pem',
    '/public/key/path/second.pem'
));
$filter->setPassphrase('mypassphrase');

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.

// Use openssl and provide a private key
$filter = new Zend_Filter_Encrypt(array(
    'adapter' => 'openssl',
    'private' => '/path/to/mykey/private.pem'
));

// of course you can also give the public keys at initiation
$filter->setPublicKey(array(
    '/public/key/path/first.pem',
    '/public/key/path/second.pem'
));
$filter->setPassphrase('mypassphrase');

$encrypted = $filter->filter('text_to_be_encoded');
$envelope  = $filter->getEnvelopeKey();
print $encrypted;

// For decryption look at the Decrypt filter

22.2.10. HtmlEntities

Returns the string $value, converting characters to their corresponding HTML entity equivalents where they exist.

22.2.11. Int

Returns (int) $value

22.2.12. LocalizedToNormalized

This 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.

[Note] Note

Please note that normalization is not equal to translation. This filter can not translate strings from one language into another like you could expect with months or names of days.

The following input types can be normalized:

  • integer: Integer numbers, which are localized, will be normalized to the english notation.

  • float: Float numbers, which are localized, will be normalized to the english notation.

  • numbers: Other numbers, like real, will be normalized to the english notation.

  • time: Time values, will be normalized to a named array.

  • date: Date values, will be normalized to a named array.

Any other input will be returned as it, without changing it.

[Note] Note

You should note that normalized output is always given as string. Otherwise your environment would transfer the normalized output automatically to the notation used by the locale your environment is set to.

22.2.12.1. Normalization for numbers

Any 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:

// Initiate the filter
$filter = new Zend_Filter_LocalizedToNormalized();
$filter->filter('123.456,78');
// returns the value '123456.78'

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:

  • date_format

  • locale

  • precision

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:

// Numeric Filter
$filter = new Zend_Filter_LocalizedToNormalized(array('precision' => 2));

$filter->filter('123.456');
// returns the value '123456.00'

$filter->filter('123.456,78901');
// returns the value '123456.79'

22.2.12.2. Normalization for date and time

Input 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.

// Initiate the filter
$filter = new Zend_Filter_LocalizedToNormalized();
$filter->filter('12.April.2009');
// returns array('day' => '12', 'month' => '04', 'year' => '2009')

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.

// Date Filter
$filter = new Zend_Filter_LocalizedToNormalized(
    array('date_format' => 'ss:mm:HH')
);

$filter->filter('11:22:33');
// returns array('hour' => '33', 'minute' => '22', 'second' => '11')

22.2.13. NormalizedToLocalized

This 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.

[Note] Note

Please note that localization is not equal to translation. This filter can not translate strings from one language into another like you could expect with months or names of days.

The following input types can be localized:

  • integer: Integer numbers, which are normalized, will be localized to the set notation.

  • float: Float numbers, which are normalized, will be localized to the set notation.

  • numbers: Other numbers, like real, will be localized to the set notation.

  • time: Time values, will be localized to a string.

  • date: Date values, will be normalized to a string.

Any other input will be returned as it, without changing it.

22.2.13.1. Localization for numbers

Any 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:

// Initiate the filter
$filter = new Zend_Filter_NormalizedToLocalized();
$filter->filter(123456.78);
// returns the value '123.456,78'

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:

  • date_format

  • locale

  • precision

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:

// Numeric Filter
$filter = new Zend_Filter_NormalizedToLocalized(array('precision' => 2));

$filter->filter(123456);
// returns the value '123.456,00'

$filter->filter(123456.78901);
// returns the value '123.456,79'

22.2.13.2. Localization for date and time

Normalized 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.

// Initiate the filter
$filter = new Zend_Filter_NormalizedToLocalized();
$filter->filter(array('day' => '12', 'month' => '04', 'year' => '2009');
// returns '12.04.2009'

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.

// Date Filter
$filter = new Zend_Filter_LocalizedToNormalized(
    array('date_format' => 'ss:mm:HH')
);

$filter->filter(array('hour' => '33', 'minute' => '22', 'second' => '11'));
// returns '11:22:33'

22.2.14. Null

This 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.

22.2.14.1. Default behaviour for Zend_Filter_Null

Per 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.

$filter = new Zend_Filter_Null();
$value  = '';
$result = $filter->filter($value);
// returns null instead of the empty string

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.

22.2.14.2. Changing behaviour for Zend_Filter_Null

Sometimes 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:

  • boolean: Converts a boolean FALSE value to NULL.

  • integer: Converts an integer 0 value to NULL.

  • empty_array: Converts an empty array to NULL.

  • string: Converts an empty string '' to NULL.

  • zero: Converts a string containing the single character zero ('0') to NULL.

  • all: Converts all above types to NULL. (This is the default behavior.)

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:

// converts false to null
$filter = new Zend_Filter_Null(Zend_Filter_Null::BOOLEAN);

// converts false and 0 to null
$filter = new Zend_Filter_Null(
    Zend_Filter_Null::BOOLEAN + Zend_Filter_Null::INTEGER
);

// converts false and 0 to null
$filter = new Zend_Filter_Null( array(
    Zend_Filter_Null::BOOLEAN,
    Zend_Filter_Null::INTEGER
));

// converts false and 0 to null
$filter = new Zend_Filter_Null(array(
    'boolean',
    'integer',
));

You can also give an instance of Zend_Config to set the wished types. To set types afterwards use setType().

22.2.15. StripNewlines

Returns the string $value without any newline control characters.

22.2.16. RealPath

This 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.

$filter = new Zend_Filter_RealPath();
$path   = '/www/var/path/../../mypath';
$filtered = $filter->filter($path);

// returns '/www/mypath'

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.

$filter = new Zend_Filter_RealPath(false);
$path   = '/www/var/path/../../non/existing/path';
$filtered = $filter->filter($path);

// returns '/www/non/existing/path'
// even when file_exists or realpath would return false

22.2.17. StringToLower

This filter converts any input to be lowercased.

$filter = new Zend_Filter_StringToLower();

print $filter->filter('SAMPLE');
// returns "sample"

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.

// using UTF-8
$filter = new Zend_Filter_StringToLower('UTF-8');

// or give an array which can be useful when using a configuration
$filter = new Zend_Filter_StringToLower(array('encoding' => 'UTF-8'));

// or do this afterwards
$filter->setEncoding('ISO-8859-1');
[Note] Setting wrong encodings

Be aware that you will get an exception when you want to set an encoding and the mbstring extension is not available in your environment.

Also when you are trying to set an encoding which is not supported by your mbstring extension you will get an exception.

22.2.18. StringToUpper

This filter converts any input to be uppercased.

$filter = new Zend_Filter_StringToUpper();

print $filter->filter('Sample');
// returns "SAMPLE"

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.

$filter = new Zend_Filter_StringToUpper(array('encoding' => 'UTF-8'));

// or do this afterwards
$filter->setEncoding('ISO-8859-1');

22.2.19. StringTrim

Returns the string $value with characters stripped from the beginning and end.

22.2.20. StripTags

This 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. Finally, this filter offers control over whether comments (e.g., <!-- ... -->) are removed or allowed.