Zend Framework comes with a standard set of filters, which are ready for you to use.
Zend_Filter_Alnum is a filter which returns only alphabetic characters and digits. All other characters are supressed.
The following options are supported for Zend_Filter_Alnum:
See the following example for the default behaviour of this filter.
1 2 3 | $filter = new Zend_Filter_Alnum();
$return = $filter->filter('This is (my) content: 123');
// returns 'Thisismycontent123'
|
The above example returns ‘Thisismycontent123’. As you see all whitespaces and also the brackets are filtered.
Note
Zend_Filter_Alnum works on almost all languages. But actually there are three exceptions: Chinese, Japanese and Korean. Within these languages the english alphabet is use instead of the characters from these languages. The language itself is detected by using Locale.
Zend_Filter_Alnum can also allow whitespaces. This can be usefull when you want to strip special chars from a text. See the following example:
1 2 3 | $filter = new Zend_Filter_Alnum(array('allowwhitespace' => true));
$return = $filter->filter('This is (my) content: 123');
// returns 'This is my content 123'
|
The above example returns ‘This is my content 123’. As you see only the brackets are filtered whereas the whitespaces are not touched.
To change allowWhiteSpace afterwards you can use setAllowWhiteSpace() and getAllowWhiteSpace().
Zend_Filter_Alpha is a filter which returns the string $value, removing all but alphabetic characters. This filter includes an option to also allow white space characters.
The following options are supported for Zend_Filter_Alpha:
A basic example of usage is below:
1 2 3 | $filter = new Zend_Filter_Alpha();
print $filter->filter('This is (my) content: 123');
|
The above example returns ‘Thisismycontent’. Notice that the whitespace characters and brackets are removed.
Note
Zend_Filter_Alpha works on most languages; however, there are three exceptions: Chinese, Japanese and Korean. With these languages the english alphabet is used. The language is detected through the use of Locale.
Zend_Filter_Alpha can also allow whitespace characters. This can be useful when you want to strip special characters from a string. See the following example:
1 2 3 | $filter = new Zend_Filter_Alpha(array('allowwhitespace' => true));
print $filter->filter('This is (my) content: 123');
|
The above example returns ‘This is my content ‘. Notice that the parenthesis, colon, and numbers have all been removed while the whitespace characters remain.
To change allowWhiteSpace after instantiation the method setAllowWhiteSpace() may be used.
To query the current value of allowWhiteSpace the method getAllowWhiteSpace() may be used.
Zend_Filter_BaseName allows you to filter a string which contains the path to a file and it will return the base name of this file.
There are no additional options for Zend_Filter_BaseName.
A basic example of usage is below:
1 2 3 | $filter = new Zend_Filter_BaseName();
print $filter->filter('/vol/tmp/filename');
|
This will return ‘filename’.
1 2 3 | $filter = new Zend_Filter_BaseName();
print $filter->filter('/vol/tmp/filename.txt');
|
This will return ‘filename.txt‘.
This filter changes a given input to be a BOOLEAN value. This is often useful when working with databases or when processing form values.
The following options are supported for Zend_Filter_Boolean:
By 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.
1 2 3 4 | $filter = new Zend_Filter_Boolean();
$value = '';
$result = $filter->filter($value);
// returns false
|
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.
Sometimes 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | // converts 0 to false
$filter = new Zend_Filter_Boolean(Zend_Filter_Boolean::INTEGER);
// converts 0 and '0' to false
$filter = new Zend_Filter_Boolean(
Zend_Filter_Boolean::INTEGER + Zend_Filter_Boolean::ZERO
);
// converts 0 and '0' to false
$filter = new Zend_Filter_Boolean(array(
'type' => array(
Zend_Filter_Boolean::INTEGER,
Zend_Filter_Boolean::ZERO,
),
));
// converts 0 and '0' to false
$filter = new Zend_Filter_Boolean(array(
'type' => array(
'integer',
'zero',
),
));
|
You can also give an instance of Zend_Config to set the desired types. To set types after instantiation, use the setType() method.
As 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().
1 2 3 4 5 6 7 8 9 10 11 12 | $filter = new Zend_Filter_Boolean(array(
'type' => Zend_Filter_Boolean::ALL,
'locale' => 'de',
));
// returns false
echo $filter->filter('nein');
$filter->setLocale('en');
// returns true
$filter->filter('yes');
|
Sometimes 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
Type | True | False |
---|---|---|
Zend_Filter_Boolean::BOOLEAN | TRUE | FALSE |
Zend_Filter_Boolean::INTEGER | 0 | 1 |
Zend_Filter_Boolean::FLOAT | 0.0 | 1.0 |
Zend_Filter_Boolean::STRING | “” | |
Zend_Filter_Boolean::ZERO | “0” | “1” |
Zend_Filter_Boolean::EMPTY_ARRAY | array() | |
Zend_Filter_Boolean::NULL | NULL | |
Zend_Filter_Boolean::FALSE_STRING | “false” (case independently) | “true” (case independently) |
Zend_Filter_Boolean::YES | localized “yes” (case independently) | localized “no” (case independently) |
The following example shows the behaviour when changing the casting option:
1 2 3 4 5 6 7 8 9 10 11 12 13 | $filter = new Zend_Filter_Boolean(array(
'type' => Zend_Filter_Boolean::ALL,
'casting' => false,
));
// returns false
echo $filter->filter(0);
// returns true
echo $filter->filter(1);
// returns the value
echo $filter->filter(2);
|
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.
The following options are supported for Zend_Filter_Callback:
The usage of this filter is quite simple. Let’s expect we want to create a filter which reverses a string.
1 2 3 4 | $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.
1 2 3 4 5 6 7 8 9 | // 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().
Note
Possible exceptions
You should note that defining a callback method which can not be called will raise an exception.
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.
1 2 3 4 5 6 7 | $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:
1 | $value = MyMethod('Hello', 'param1', 'param2');
|
These two filters are capable of compressing and decompressing strings, files, and directories.
The following options are supported for Zend_Filter_Compress and Zend_Filter_Decompress:
The following compression formats are supported by their own adapter:
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)
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.
1 | $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).
1 2 3 4 5 6 | $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.
1 | $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.
1 2 3 | $filter = new Zend_Filter_Compress('Bz2');
$compressed = $filter->filter('Uncompressed string');
// Returns the compressed string
|
Decompression works the same way.
1 2 3 | $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.
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.
1 2 3 4 5 6 7 8 | $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.
1 2 3 4 5 6 7 8 | $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.
1 2 3 4 5 6 7 8 | $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.
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.
1 2 3 | $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.
1 2 3 4 5 6 7 8 9 | $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.
The 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.
The 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.
The Lzf Adapter can compress and decompress:
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.
The Rar Adapter can compress and decompress:
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:
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.
The Tar Adapter can compress and decompress:
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:
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.
The Zip Adapter can compress and decompress:
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:
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.
Returns the string $value, removing all but digits.
There are no additional options for Zend_Filter_Digits.
A basic example of usage is below:
1 2 3 | $filter = new Zend_Filter_Digits();
print $filter->filter('October 2009');
|
This returns “2009”.
1 2 3 | $filter = new Zend_Filter_Digits();
print $filter->filter('HTML 5 for Dummies');
|
This returns “5”.
Given a string containing a path to a file, this function will return the name of the directory.
There are no additional options for Zend_Filter_Dir.
A basic example of usage is below:
1 2 3 | $filter = new Zend_Filter_Dir();
print $filter->filter('/etc/passwd');
|
This returns “/etc”.
1 2 3 | $filter = new Zend_Filter_Dir();
print $filter->filter('C:/Temp/x');
|
This returns “C:/Temp”.
These filters allow to encrypt and decrypt any given string. Therefor they make use of Adapters. Actually there are adapters for the Mcrypt and OpenSSL extensions from PHP.
The following options are supported for Zend_Filter_Encrypt and Zend_Filter_Decrypt:
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.
1 2 3 4 5 | // 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.
1 2 3 | // Use the Mcrypt adapter
$filter = new Zend_Filter_Encrypt();
$filter->setAdapter('openssl');
|
Note
When you do not supply the adapter option or do not use setAdapter(), then the Mcrypt adapter will be used per default.
When you have installed the Mcrypt extension you can use the Mcrypt adapter. If you provide a string instead of an array of options, this string will be used as key.
You can get and set the encryption values also afterwards with the getEncryption() and setEncryption() methods.
Note
Note that you will get an exception if the mcrypt extension is not available in your environment.
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 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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 | // 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
|
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 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.
1 2 3 4 5 6 7 8 | // 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 that you will get an exception if the mcrypt extension is not available in your environment.
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.
When you have installed the OpenSSL extension you can use the OpenSSL adapter. 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.
1 2 3 4 5 6 7 8 9 10 11 | // 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 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.
1 2 3 4 5 6 7 8 9 10 11 12 | // 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | // 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
|
As 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.
1 2 3 4 5 6 7 8 9 10 11 12 | // Use openssl and provide a private key
$filter = new Zend_Filter_Encrypt(array(
'adapter' => 'openssl',
'private' => '/path/to/mykey/private.pem',
'public' => '/public/key/path/public.pem',
'package' => true
));
$encrypted = $filter->filter('text_to_be_encoded');
print $encrypted;
// For decryption look at the Decrypt filter
|
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.
Based 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | // Use basic compression adapter
$filter1 = new Zend_Filter_Encrypt(array(
'adapter' => 'openssl',
'private' => '/path/to/mykey/private.pem',
'public' => '/public/key/path/public.pem',
'package' => true,
'compression' => 'bz2'
));
// Use basic compression adapter
$filter2 = new Zend_Filter_Encrypt(array(
'adapter' => 'openssl',
'private' => '/path/to/mykey/private.pem',
'public' => '/public/key/path/public.pem',
'package' => true,
'compression' => array('adapter' => 'zip', 'target' => '\usr\tmp\tmp.zip')
));
|
Note
Decryption with same settings
When you want to decrypt a value which is additionally compressed, then you need to set the same compression settings for decryption as for encryption. Otherwise the decryption will fail.
Decryption with OpenSSL is as simple as encryption. But you need to have all data from the person who encrypted the content. See the following example:
1 2 3 4 5 6 7 8 9 10 11 | // 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 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.
1 2 3 4 5 6 7 8 9 10 11 12 | // 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // 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;
|
Returns the string $value, converting characters to their corresponding HTML entity equivalents where they exist.
The following options are supported for Zend_Filter_HtmlEntities:
quotestyle: Equivalent to the PHP htmlentities native function parameter quote_style. This allows you to define what will be done with ‘single’ and “double” quotes. The following constants are accepted: ENT_COMPAT, ENT_QUOTES ENT_NOQUOTES with the default being ENT_COMPAT.
charset: Equivalent to the PHP htmlentities native function parameter charset. This defines the character set to be used in filtering. Unlike the PHP native function the default is ‘UTF-8’. See “http://php.net/htmlentities” for a list of supported character sets.
Note
This option can also be set via the $options parameter as a Zend_Config object or array. The option key will be accepted as either charset or encoding.
doublequote: Equivalent to the PHP htmlentities native function parameter double_encode. If set to false existing html entities will not be encoded. The default is to convert everything (true).
Note
This option must be set via the $options parameter or the setDoubleEncode() method.
See the following example for the default behaviour of this filter.
1 2 3 | $filter = new Zend_Filter_HtmlEntities();
print $filter->filter('<');
|
Zend_Filter_HtmlEntities allows changing the quote style used. This can be useful when you want to leave double, single, or both types of quotes un-filtered. See the following example:
1 2 3 4 | $filter = new Zend_Filter_HtmlEntities(array('quotestyle' => ENT_QUOTES));
$input = "A 'single' and " . '"double"';
print $filter->filter($input);
|
The above example returns A ‘single’ and “double”. Notice that ‘single’ as well as “double” quotes are filtered.
1 2 3 4 | $filter = new Zend_Filter_HtmlEntities(array('quotestyle' => ENT_COMPAT));
$input = "A 'single' and " . '"double"';
print $filter->filter($input);
|
The above example returns A ‘single’ and “double”. Notice that “double” quotes are filtered while ‘single’ quotes are not altered.
1 2 3 4 | $filter = new Zend_Filter_HtmlEntities(array('quotestyle' => ENT_NOQUOTES));
$input = "A 'single' and " . '"double"';
print $filter->filter($input);
|
The above example returns A ‘single’ and “double”. Notice that neither “double” or ‘single’ quotes are altered.
To change or retrieve the quotestyle after instantiation, the two methods setQuoteStyle() and getQuoteStyle() may be used respectively. setQuoteStyle() accepts one parameter $quoteStyle. The following constants are accepted: ENT_COMPAT, ENT_QUOTES, ENT_NOQUOTES
1 2 3 4 | $filter = new Zend_Filter_HtmlEntities();
$filter->setQuoteStyle(ENT_QUOTES);
print $filter->getQuoteStyle(ENT_QUOTES);
|
To change or retrieve the charset after instantiation, the two methods setCharSet() and getCharSet() may be used respectively. setCharSet() accepts one parameter $charSet. See “http://php.net/htmlentities” for a list of supported character sets.
1 2 3 4 | $filter = new Zend_Filter_HtmlEntities();
$filter->setQuoteStyle(ENT_QUOTES);
print $filter->getQuoteStyle(ENT_QUOTES);
|
To change or retrieve the doublequote option after instantiation, the two methods setDoubleQuote() and getDoubleQuote() may be used respectively. setDoubleQuote() accepts one boolean parameter $doubleQuote.
1 2 3 4 | $filter = new Zend_Filter_HtmlEntities();
$filter->setQuoteStyle(ENT_QUOTES);
print $filter->getQuoteStyle(ENT_QUOTES);
|
Zend_Filter_Int allows you to transform a sclar value which contains into an integer.
There are no additional options for Zend_Filter_Int.
A basic example of usage is below:
1 2 3 | $filter = new Zend_Filter_Int();
print $filter->filter('-4 is less than 0');
|
This will return ‘-4’.
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.
The following options are supported 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.
1 2 3 4 | $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.
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:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | // 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().
Zend_Filter_PregReplace performs a search using regular expressions and replaces all found elements.
The following options are supported for Zend_Filter_PregReplace:
To use this filter properly you must give two options:
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.
1 2 3 4 5 6 7 8 | $filter = new Zend_Filter_PregReplace(array(
'match' => '/bob/',
'replace' => 'john',
));
$input = 'Hy bob!";
$filter->filter($input);
// returns 'Hy john!'
|
You can use getMatchPattern() and setMatchPattern() to set the matching pattern afterwards. To set the replacement pattern you can use getReplacement() and setReplacement().
1 2 3 4 5 6 7 | $filter = new Zend_Filter_PregReplace();
$filter->setMatchPattern(array('bob', 'Hy'))
->setReplacement(array('john', 'Bye'));
$input = 'Hy bob!";
$filter->filter($input);
// returns 'Bye john!'
|
For a more complex usage take a look into PHP‘s PCRE Pattern Chapter.
This filter will resolve given links and pathnames and returns canonicalized absolute pathnames.
The following options are supported for Zend_Filter_RealPath:
For any given link of pathname its absolute path will be returned. 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.
1 2 3 4 5 | $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.
1 2 3 4 5 6 | $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
|
This filter converts any input to be lowercased.
The following options are supported for Zend_Filter_StringToLower:
This is a basic example:
1 2 3 4 | $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.
1 2 3 4 5 6 7 8 | // 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.
This filter converts any input to be uppercased.
The following options are supported for Zend_Filter_StringToUpper:
This is a basic example for using the StringToUpper filter:
1 2 3 4 | $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.
1 2 3 4 | $filter = new Zend_Filter_StringToUpper(array('encoding' => 'UTF-8'));
// or do this afterwards
$filter->setEncoding('ISO-8859-1');
|
This filter modifies a given string such that certain characters are removed from the beginning and end.
The following options are supported for Zend_Filter_StringTrim:
A basic example of usage is below:
1 2 3 | $filter = new Zend_Filter_StringTrim();
print $filter->filter(' This is (my) content: ');
|
The above example returns ‘This is (my) content:’. Notice that the whitespace characters have been removed.
1 2 3 4 | $filter = new Zend_Filter_StringTrim(':');
// or new Zend_Filter_StringTrim(array('charlist' => ':'));
print $filter->filter(' This is (my) content:');
|
The above example returns ‘This is (my) content’. Notice that the whitespace characters and colon are removed. You can also provide an instance of Zend_Config or an array with a ‘charlist’ key. To set the desired character list after instantiation, use the setCharList() method. The getCharList() return the values set for charlist.
The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.