標準のフィルタクラス群

Zend Framework には、すぐに使える標準のフィルタ群が同梱されています。

Alnum(日本語)

Zend_Filter_Alnum は、 アルファベットおよび数字だけを返すフィルターです。 他の全ての文字は抑止されます。 (訳注:ここでいうアルファベットおよび数字、とは各言語で使われる文字および数字の集合を意味します。英数字ではありません

Zend_Filter_Alnum でサポートされるオプション

下記のオプションが Zend_Filter_Alnum でサポートされます。

  • allowwhitespace: このオプションが設定されていると、空白文字が許されます。 設定されていない場合は抑止されます。既定値では、空白は許されません。

基本的な使用法

このフィルターの既定の振る舞いについては、下記の例をご覧ください。

$filter = new Zend_Filter_Alnum();
$return = $filter->filter('This is (my) content: 123');
// 'Thisismycontent123' を返します

上記の例は 'Thisismycontent123' を返します。 ご覧の通り、空白の全てと、括弧がフィルターされています。

注記

Zend_Filter_Alnum はほとんどの言語で動作します。 しかし実際には、3つの例外、中国語、日本語、Koreanがあります。 これらの言語では、英語のアルファベットがそれらの言語の文字の替わりに使用されます。 この言語自身は Zend_Locale を使って検出されます。

空白を許す

Zend_Filter_Alnum は空白も許します。 テキストから特別な文字を取り除きたい場合に役立つでしょう。下記の例をご覧ください。

$filter = new Zend_Filter_Alnum(array('allowwhitespace' => true));
$return = $filter->filter('This is (my) content: 123');
// 'This is my content 123' を返します。

上記の例は 'This is my content 123' を返します。 ご覧の通り、括弧はフィルターされましたが、空白には手が出されていません。

あとで allowWhiteSpace を変更するには、 setAllowWhiteSpace()getAllowWhiteSpace() を使用できます。

Alpha(日本語)

Zend_Filter_Alpha は、 アルファベットを残して全てを削除した、文字列 $value を返すフィルターです。このフィルターには、空白文字を許すオプションもあります。 (訳注:ここでいうアルファベット、とはアラビア文字やタイ文字、キリール文字など、各言語で使われる文字の集合を意味します。英字とは限りません

Zend_Filter_Alpha でサポートされるオプション

下記のオプションが Zend_Filter_Alpha でサポートされます。

  • allowwhitespace: このオプションが設定されていると、空白文字が許されます。 設定されていない場合は抑止されます。既定値では、空白文字は許されません。

基本的な使用法

下記は基本的な使用例です。

$filter = new Zend_Filter_Alpha();

print $filter->filter('This is (my) content: 123');

上記の例は 'Thisismycontent' を返します。 空白文字と括弧が削除されていることに注意してください。

注記

Zend_Filter_Alpha はほとんどの言語で動作します。 しかしながら、3つの例外、中国語、日本語、Koreanがあります。 これらの言語では、英語のアルファベットが使用されます。 言語は Zend_Locale の使用を通じて検出されます。

空白文字を許す

Zend_Filter_Alpha は空白文字も許せます。 文字列から特別な文字を取り除きたい場合に役立つでしょう。 下記の例をご覧ください。

$filter = new Zend_Filter_Alpha(array('allowwhitespace' => true));

print $filter->filter('This is (my) content: 123');

上記の例は 'This is my content ' を返します。 括弧やコロン、数字が全て除去された一方で、空白文字が残っていることに注意してください。

メソッド初期化後に allowWhiteSpace を変更するには、 setAllowWhiteSpace() が使えるでしょう。

メソッドの現在の allowWhiteSpace の値を照会するには、 getAllowWhiteSpace() が使えるでしょう。

BaseName(日本語)

Zend_Filter_BaseName により、 ファイルへのパスを含む文字列をフィルタできます。 このファイルの基盤名を返します。

Zend_Filter_BaseName でサポートされるオプション

Zend_Filter_BaseName に対する追加オプションはありません。

基本的な使用法

下記は基本的な使用例です。

$filter = new Zend_Filter_BaseName();

print $filter->filter('/vol/tmp/filename');

これは 'filename' を返します。

$filter = new Zend_Filter_BaseName();

print $filter->filter('/vol/tmp/filename.txt');

これは 'filename.txt' を返します。

Boolean

This filter changes a given input to be a BOOLEAN value. This is often useful when working with databases or when processing form values.

Supported options for Zend_Filter_Boolean

The following options are supported for Zend_Filter_Boolean:

  • casting: When this option is set to TRUE then any given input will be casted to boolean. This option defaults to TRUE.

  • locale: This option sets the locale which will be used to detect localized input.

  • type: The type option sets the boolean type which should be used. Read the following for details.

Default behaviour 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.

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

Changing behaviour for Zend_Filter_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:

  • boolean: Returns a boolean value as is.

  • integer: Converts an integer 0 value to FALSE.

  • float: Converts a float 0.0 value to FALSE.

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

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

  • empty_array: Converts an empty array to FALSE.

  • null: Converts a NULL value to FALSE.

  • php: Converts values according to PHP when casting them to BOOLEAN.

  • false_string: Converts a string containing the word "false" to a boolean FALSE.

  • yes: Converts a localized string which contains the word "no" to FALSE.

  • all: Converts all above types to BOOLEAN.

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:

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

Localized booleans

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

$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');

Disable casting

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

表75 Usage without casting

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:

$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);

Callback(日本語)

このフィルタにより、 Zend_Filter とともに自分自身のメソッドを使えます。 機能を果たすメソッドがすでにあるとき、新しいフィルタを生成する必要はありません。

Zend_Filter_Callback でサポートされるオプション

Zend_Filter_Callback では、下記のオプションがサポートされます。

  • callback: 使用されるべきコールバックを設定します。

  • options: コールバックが処理される際に使われるオプションを設定します。

基本的な使用法

このフィルタの使い方はとても簡単です。 文字列を逆にするフィルタを生成したいとしましょう。

$filter = new Zend_Filter_Callback('strrev');

print $filter->filter('Hello!');
// "!olleH"を返します

おわかりのように、自分自身のフィルタを定義するために本当に簡単にコールバックを使えます。 メソッド(それはクラス内で定義されます)をコールバックとして配列を与えることによって使うこともできます。

// クラスの定義
class MyClass
{
    public function Reverse($param);
}

// フィルター定義
$filter = new Zend_Filter_Callback(array('MyClass', 'Reverse'));
print $filter->filter('Hello!');

実際に設定されているコールバックを取得するには getCallback() を使い、 他のコールバックを設定するには setCallback() を使います。

起こりうる例外

呼ばれることができないコールバック・メソッドを定義すると、 例外が発生する点に注意しなければなりません。

Default parameters within a callback

フィルタが実行されるとき、 呼ばれるメソッドに配列として与えられるデフォルト・パラメータを定義できます。 この配列は、フィルターされた値で結合されます。

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

手動で上記のメソッド定義を呼ぶと、それはこのように見えます:

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

Compress and Decompress

These two filters are capable of compressing and decompressing strings, files, and directories.

Supported options for Zend_Filter_Compress and Zend_Filter_Decompress

The following options are supported for Zend_Filter_Compress and Zend_Filter_Decompress:

  • adapter: The compression adapter which should be used. It defaults to Gz.

  • options: Additional options which are given to the adapter at initiation. Each adapter supports it's own options.

Supported compression adapters

The following compression formats are supported by their own adapter:

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

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,
    ),
));

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

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.

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

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

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

Directories to extract to must exist

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

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.

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.

Lzf Adapter

The Lzf Adapter can compress and decompress:

  • Strings

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.

Rar Adapter

The Rar Adapter can compress and decompress:

  • Files

  • Directories

Rar does not support strings

The Rar Adapter can not handle strings.

This adapter makes use of PHP's Rar extension.

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.

Tar Adapter

The Tar Adapter can compress and decompress:

  • Files

  • Directories

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.

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.

Zip Adapter

The Zip Adapter can compress and decompress:

  • Strings

  • Files

  • Directories

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.

数字

文字列 $value を返します。数字以外の全てを除去します。

Zend_Filter_Digits でサポートされるオプション

Zend_Filter_Digits に対する追加オプションはありません。

基本的な使用法

下記は基本的な使用例です。

$filter = new Zend_Filter_Digits();

print $filter->filter('October 2009');

これは "2009" を返します。

$filter = new Zend_Filter_Digits();

print $filter->filter('HTML 5 for Dummies');

これは "5" を返します。

Dir(日本語)

ファイルへのパスを含む文字列が与えられると、この関数は、ディレクトリ名を返します。

Zend_Filter_Dir でサポートされるオプション

Zend_Filter_Dir に対する追加オプションはありません。

基本的な使用法

下記は基本的な使用例です。

$filter = new Zend_Filter_Dir();

print $filter->filter('/etc/passwd');

これは "/etc" を返します。

$filter = new Zend_Filter_Dir();

print $filter->filter('C:/Temp/x');

これは "C:/Temp" を返します。

Encrypt 及び Decrypt

これらのフィルタにより、与えられた文字列を暗号化したり復号化できます。 したがって、それらはアダプタを利用します。 実際には、PHP 由来の Mcrypt および OpenSSL エクステンション用のアダプタを使用します。

Zend_Filter_Encrypt および Zend_Filter_Decrypt でサポートされるオプション

Zend_Filter_Encrypt および Zend_Filter_Decrypt では、下記のオプションがサポートされます。

  • adapter: 使用する暗号化アダプタを設定します。

  • algorithm: MCrypt のみ。 使用するアルゴリズム PHP の mcrypt 暗号 であげられている暗号化アルゴリズムのいずれかでなければなりません。 省略した場合のデフォルトは blowfish です。

  • algorithm_directory: MCrypt のみ。 アルゴリズムが存在するディレクトリ。 省略した場合のデフォルトは、mcrypt エクステンション内に設定されたパスです。

  • compression: 暗号化した値を圧縮するかどうか。既定値は圧縮なし。

  • envelope: OpenSSL のみ。 コンテンツを暗号化した人から受け取った、 暗号化されたエンベロープ鍵。 鍵ファイルのパスとファイル名を指定するか、 あるいは単に鍵ファイルの内容そのものを指定することもできます。 package オプションが設定された場合、 このパラメータは省略できます。

  • key: MCrypt のみ。 暗号化用の鍵。 入力を暗号化する際に使用します。 復号する際にも同じ鍵が必要です。

  • mode: MCrypt のみ。 使用する暗号化モード。 PHP の mcrypt 暗号 であげられているモードのいずれかでなければなりません。 省略した場合のデフォルトは 'cbc' です。

  • mode_directory: MCrypt のみ。 モードが存在するディレクトリ。 省略した場合のデフォルトは、Mcrypt 拡張モジュールで設定されているパスです。

  • package: OpenSSL のみ。 暗号化された値と一緒にエンベロープ鍵が梱包されるべきかどうか デフォルトは FALSE です。

  • private: OpenSSL のみ。 コンテンツの暗号化に使用する、あなたの秘密鍵。 鍵ファイルのパスとファイル名を指定するか、 あるいは単に鍵ファイルの内容そのものを指定することもできます。

  • public: OpenSSL のみ。 暗号化したコンテンツを渡したい相手の公開鍵。 複数の公開鍵を指定するには、配列を使用します。 鍵ファイルのパスとファイル名を指定するか、 あるいは単に鍵ファイルの内容そのものを指定することもできます。

  • salt: MCrypt のみ。 キーを salt 値として使用するかどうか。 使用すると、暗号化に使用するキー自体も暗号化されます。 デフォルトは FALSE です。

  • vector: MCrypt のみ。 使用する初期化ベクトル。 省略した場合はランダムなベクトルとなります。

アダプタの使用法

これら 2 つの暗号化手法はまったく異なる方式なので、 アダプタの使用法もそれぞれ異なります。 フィルタを初期化する際に、どのアダプタを使うかを選ばなければなりません。

// Mcrypt アダプタを使用します
$filter1 = new Zend_Filter_Encrypt(array('adapter' => 'mcrypt'));

// OpenSSL アダプタを使用します
$filter2 = new Zend_Filter_Encrypt(array('adapter' => 'openssl'));

別のアダプタを設定するために setAdapter() を使用することもできます。また getAdapter() メソッドで、実際に設定されているアダプタを取得できます。

// Mcrypt アダプタを使用します
$filter = new Zend_Filter_Encrypt();
$filter->setAdapter('openssl');

注記

adapter オプションを指定しなかったり setAdapter() を使用しなかったりした場合は、デフォルトで Mcrypt アダプタを使用します。

Mcrypt での暗号化

Mcrypt 拡張モジュールをインストールすると、 Mcrypt アダプタが使えるようになります。 もしオプション配列の代わりに文字列を与えると、 文字列がキーとして使われます。

初期化した後で暗号化の値を取得したり設定したりするには、それぞれ getEncryption() および setEncryption() メソッドを使用します。

注記

mcrypt 拡張モジュールが使用できない場合は例外が発生することに注意しましょう。

注記

また、インスタンス作成時あるいは setEncryption() をコールした際にすべての設定がチェックされることにも注意しましょう。 設定内容に問題があることを mcrypt が検知すると、例外がスローされます。

暗号化ベクトルの取得や設定には、それぞれ getVector() および setVector() を使用可能です。指定した文字列が、 そのアルゴリズムに必要なベクトルのサイズに応じて切り詰められたり伸ばされたりします。

注記

自前のベクトル以外を使用する場合は、 そのベクトルを取得してどこかに保存しておかなければならないことに注意しましょう。 そうしないと、文字列が復号できなくなります。

// デフォルト設定の blowfish を使用します
$filter = new Zend_Filter_Encrypt('myencryptionkey');

// 自前のベクトルを設定します。それ以外の場合は getVector()
// をコールしてベクトルを保存しておかないと、後で復号できなくなります
$filter->setVector('myvector');
// $filter->getVector();

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

// 復号の方法は Decrypt フィルタを参照ください

Mcrypt の復号

Mcrypt で暗号化したコンテンツを復号するには、 暗号化を行った際に指定したオプションが必要です。

ここでひとつ暗号化時との大きな違いがあります。 暗号化の際にベクトルを指定しなかった場合は、コンテンツを暗号化した後で フィルタの getVector() メソッドを使用してベクトルを取得する必要があります。 正しいベクトルがなければ、コンテンツを復号することはできません。

オプションを指定しさえすれば、復号は暗号化と同じくらい単純なことです。

// デフォルト設定の blowfish を使用します
$filter = new Zend_Filter_Decrypt('myencryptionkey');

// コンテンツの暗号化用ベクトルを設定します
$filter->setVector('myvector');

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

注記

mcrypt 拡張モジュールが使用できない場合は例外が発生することに注意しましょう。

注記

また、インスタンス作成時あるいは setEncryption() をコールした際にすべての設定がチェックされることにも注意しましょう。 設定内容に問題があることを mcrypt が検知すると、例外がスローされます。

OpenSSL での暗号化

OpenSSL 拡張モジュールをインストールすると、 OpenSSL アダプタが使えるようになります。 後から公開鍵を取得あるいは設定するには、getPublicKey() および setPublicKey() メソッドを使用します。 秘密鍵についても、getPrivateKey() および setPrivateKey() メソッドで取得あるいは設定できます。

// openssl を使用し、秘密鍵を指定します
$filter = new Zend_Filter_Encrypt(array(
    'adapter' => 'openssl',
    'private' => '/path/to/mykey/private.pem'
));

// もちろん、初期化時に公開鍵を指定することもできます
$filter->setPublicKey(array(
    '/public/key/path/first.pem',
    '/public/key/path/second.pem'
));

注記

OpenSSL アダプタは、正しい鍵を渡さないと動作しないことに注意しましょう。

鍵自体も暗号化したい場合は、パスフレーズを setPassphrase() メソッドで渡します。 パスフレーズつきで暗号化したコンテンツを復号したい場合は、 公開鍵だけではなく (暗号化された鍵を復号するための) パスフレーズも必要となります。

// openssl を使用し、秘密鍵を指定します
$filter = new Zend_Filter_Encrypt(array(
    'adapter' => 'openssl',
    'private' => '/path/to/mykey/private.pem'
));

// もちろん、初期化時に公開鍵を指定することもできます
$filter->setPublicKey(array(
    '/public/key/path/first.pem',
    '/public/key/path/second.pem'
));
$filter->setPassphrase('mypassphrase');

最後に、OpenSSL を使用した場合に受け手に渡す必要があるものをまとめます。 暗号化されたコンテンツ、パスフレーズを使用した場合はそのパスフレーズ、 そして復号用のエンベロープ鍵。これらが必要となります。

つまり、暗号化を終えたらエンベロープ鍵を取得する必要があるということです。 取得するには getEnvelopeKey() メソッドを使用します。

OpenSSL でコンテンツの暗号化を行う完全な例は、このようになります。

// openssl を使用し、秘密鍵を指定します
$filter = new Zend_Filter_Encrypt(array(
    'adapter' => 'openssl',
    'private' => '/path/to/mykey/private.pem'
));

// もちろん、初期化時に公開鍵を指定することもできます
$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;

// 復号の方法は Decrypt フィルタを参照ください
Openssl と一緒に簡単に使う方法

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.

// openssl を使用し、秘密鍵を指定します
$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;

// 復号の方法は Decrypt フィルタを参照ください

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.

// 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')
));

同一設定での復号化

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.

OpenSSL での復号

OpenSSL での復号は、暗号化と同様にシンプルです。 しかし、コンテンツを暗号化した人からすべてのデータを受け取る必要があります。 下記の例をご覧ください。

// openssl を使用し、秘密鍵を指定します
$filter = new Zend_Filter_Decrypt(array(
    'adapter' => 'openssl',
    'private' => '/path/to/mykey/private.pem'
));

// もちろん、初期化時にエンベロープ鍵を指定することもできます
$filter->setEnvelopeKey(array(
    '/key/from/encoder/first.pem',
    '/key/from/encoder/second.pem'
));

注記

OpenSSL アダプタは、正しい鍵を渡さないと動作しないことに注意しましょう。

オプションで、パスフレーズを渡さなければ鍵を復号できないようにすることもできます。 そのために使用するのが setPassphrase() メソッドです。

// openssl を使用し、秘密鍵を指定します
$filter = new Zend_Filter_Decrypt(array(
    'adapter' => 'openssl',
    'private' => '/path/to/mykey/private.pem'
));

// もちろん、初期化時にエンベロープ鍵を指定することもできます
$filter->setEnvelopeKey(array(
    '/key/from/encoder/first.pem',
    '/key/from/encoder/second.pem'
));
$filter->setPassphrase('mypassphrase');

最後に、コンテンツを復号します。 暗号化したコンテンツの復号を行う完全な例は、このようになります。

// openssl を使用し、秘密鍵を指定します
$filter = new Zend_Filter_Decrypt(array(
    'adapter' => 'openssl',
    'private' => '/path/to/mykey/private.pem'
));

// もちろん、初期化時にエンベロープ鍵を指定することもできます
$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;

HtmlEntities

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

Supported options for Zend_Filter_HtmlEntities

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.

    注記

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

    注記

    This option must be set via the $options parameter or the setDoubleEncode() method.

Basic usage

See the following example for the default behaviour of this filter.

$filter = new Zend_Filter_HtmlEntities();

print $filter->filter('<');

Quote Style

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:

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

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

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

Helper Methods

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

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

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

$filter = new Zend_Filter_HtmlEntities();

$filter->setQuoteStyle(ENT_QUOTES);
print $filter->getQuoteStyle(ENT_QUOTES);

Int(日本語)

Zend_Filter_Int により、含んでいるスカラー値を整数に変換できます

Zend_Filter_Int でサポートされるオプション

Zend_Filter_Int に対する追加オプションはありません。

基本的な使用法

下記は基本的な使用例です。

$filter = new Zend_Filter_Int();

print $filter->filter('-4 is less than 0');

これは '-4' を返します。

LocalizedToNormalized および NormalizedToLocalized

These two filters can change given localized input to it's normalized representation and reverse. They use in Background Zend_Locale to do this transformation for you.

Zend_Filter_LocalizedToNormalized および Zend_Filter_NormalizedToLocalized でサポートされるオプション

Zend_Filter_LocalizedToNormalized および Zend_Filter_NormalizedToLocalized では、下記のオプションがサポートされます。

  • date_format: 正規化で使う日付形式や、 地域化された日付形式を検出する日付形式を設定します。

  • locale: 正規化で使うロケールや、 地域化された形式を検出するロケールを設定します。

  • precision: 数値変換で使う精度を設定します。

ワークフロー

正規化により、ユーザーがユーザー固有の言語表記で情報を入力でき、 さらに例えば正規化された値をデータベースに保存できるようになります。 一方で地域化により、 保管された正規化された値をローカルな流儀でユーザーに提示できるようになります。

注記

正規化および地域化と翻訳とは同一ではないことに注意してください。 これらのフィルタでは、月や日の名前で期待するような、 ある言語から別のものへの文字列の翻訳は行えません。

下記の入力型が正規化および地域化されます。

  • integer: 整数値。正規化では英語表記を返します。

  • float: フロート値。正規化では英語表記を返します。

  • numbers: 実数のようなその他の数値。正規化では英語表記を返します。

  • time: 時刻値。連想配列に正規化されます。

  • date: 日付値。連想配列に正規化されます。

その他の入力はいずれも変更無しにそのまま返されます。

注記

正規化された出力が常に文字列として与えられることに注意するべきです。 これ以外の場合には、 環境は正規化された出力から、 環境で設定されたロケールで使われる表記法に 自動的に変換します。

数値の正規化

整数、フロートまたは実数のようなあらゆる数が正規化されます。 指数表記の数値は、実はこのフィルタで扱えないので注意してください。

数値についての詳しい正規化方法

//フィルタ初期化
$filter = new Zend_Filter_LocalizedToNormalized();
$filter->filter('123.456,78');
//値 '123456.78' を返します。

アプリケーション全体のロケールとしてロケール 'de' を設定したつもりになりましょう。 Zend_Filter_LocalizedToNormalizedは設定されたロケールを受け取って、 どの種類の入力をあなたが与えたか検出するために、それを使います。 われわれの例ではそれは精度を持つ値でした。 そこで、この値を文字列として正規化した表現をフィルタは返します。

正規化した数がどのようになるべきか、コントロールすることもできます。 このためにZend_Locale_Formatでも使用されるオプションを 全て与えられます。利用可能なオプションについて詳しくは、この Zend_Locale の章をご覧ください。

下記はオプションの動作方法が分かるように精度を定義した例です。

//数値フィルタ
$filter = new Zend_Filter_LocalizedToNormalized(array('precision' => 2));

$filter->filter('123.456');
//値 '123456.00' を返します。

$filter->filter('123.456,78901');
//値 '123456.79' を返します。

日時の正規化

日付や時刻の値の入力もまた、正規化できます。 与えられた日付および時刻は全て、独自のキーの範囲内で日付の各部分が与えられた 配列として返されます。

//フィルタ初期化
$filter = new Zend_Filter_LocalizedToNormalized();
$filter->filter('12.April.2009');
// array('day' => '12', 'month' => '04', 'year' => '2009') を返します。

ふたたびロケール 'de' を設定したつもりになりましょう。 そこで、入力は自動的に日付として検出され、 返り値に連想配列を受け取ります。

もちろん、日付の入力値をどのようにするか date_formatlocaleオプションで コントロールすることもできます。

//日付フィルタ
$filter = new Zend_Filter_LocalizedToNormalized(
    array('date_format' => 'ss:mm:HH')
);

$filter->filter('11:22:33');
// array('hour' => '33', 'minute' => '22', 'second' => '11') を返します。

数値の地域化

整数、フロートまたは実数のようなあらゆる数が地域化されます。 指数表記の数値は、実はこのフィルタで扱えないので注意してください。

数値についての詳しい地域化方法

//フィルタを初期化
$filter = new Zend_Filter_NormalizedToLocalized();
$filter->filter(123456.78);
//値 '123.456,78' を返します。

アプリケーション全体のロケールとしてロケール 'de' を設定したつもりになりましょう。 Zend_Filter_NormalizedToLocalizedは設定されたロケールを受け取って、 どの種類の出力をあなたが受け取りたいのか検出するために、それを使います。 われわれの例ではそれは精度を持つ値でした。 そこで、この値を文字列として地域化した表現をフィルタは返します。

地域化した数がどのようになるべきか、コントロールすることもできます。 このためにZend_Locale_Formatでも使用されるオプションを 全て与えられます。 これらのオプションの使い方について詳しくは、この Zend_Locale の章 をご覧ください。

下記はオプションの動作方法が分かるように精度を定義した例です。

//数値フィルタ
$filter = new Zend_Filter_NormalizedToLocalized(array('precision' => 2));

$filter->filter(123456);
//値 '123.456,00' を返します。

$filter->filter(123456.78901);
//値 '123.456,79' を返します。

日時の地域化

日付や時刻の値を正規化したものもまた、地域化できます。 与えられた日付および時刻は全て、設定されたロケールで定義された形式で 文字列として返されます。

//フィルタを初期化
$filter = new Zend_Filter_NormalizedToLocalized();
$filter->filter(array('day' => '12', 'month' => '04', 'year' => '2009');
// '12.04.2009' を返します。

ふたたびロケール 'de' を設定したつもりになりましょう。 そこで、入力は自動的に日付として検出され、 ロケール 'de' で定義された形式で返されます。

もちろん、日付の入力値をどのようにするか date_formatlocaleオプションで コントロールすることもできます。

//日付フィルタ
$filter = new Zend_Filter_LocalizedToNormalized(
    array('date_format' => 'ss:mm:HH')
);

$filter->filter(array('hour' => '33', 'minute' => '22', 'second' => '11'));
// '11:22:33' を返します。

Null(日本語)

もし指定の基準を満たす場合、 このフィルタは与えられた入力を NULLに変更します。 データベースとともに作業するとき、これはしばしば必要です そして、ブールや他のあらゆるタイプの代わりに NULL値を持つことを望みます。

Zend_Filter_Null でサポートされるオプション

Zend_Filter_Null では、下記のオプションがサポートされます。

  • type: サポートされるべき変数の型

Zend_Filter_Null の既定の振る舞い

デフォルトではこのフィルタはPHPempty()メソッドのように動作します。 言い換えると、もしempty()がブールの TRUE を返す場合、 NULL値が返されます。

$filter = new Zend_Filter_Null();
$value  = '';
$result = $filter->filter($value);
// 空文字列の代わりに null を返します

これは、何の設定もなしに、 Zend_Filter_Nullは全ての入力を受け付けて、 empty()と同じ場合に NULLを返すことを意味します。

他の値ではいずれも変更無しにそのまま返されます。

Zend_Filter_Null の振る舞いの変更

しばしば、empty()に基づくフィルタでは十分ではありません。 そのため、Zend_Filter_Nullでは、 いずれのタイプが変換されるか否か設定できます。

下記のタイプが操作できます。

  • boolean: ブールの FALSE 値を NULL に変換します。

  • integer: 整数値の 0NULL.に変換します。

  • empty_array: 空の arrayNULL.に変換します。

  • string: 空文字列 ''NULL.に変換します。

  • zero: 単一の文字、ゼロ ('0') を含む文字列を NULL.に変換します。

  • all: 上記のタイプ全てを NULL.に変換します。 (これが既定の振る舞いです)

上記のタイプのうちのいずれをフィルタするかを選択する方法が、いくつかあります。 1つまたは複数のタイプを与えて、それに追加できます。 配列を与えたり、定数を使えます。 または、原文の文字列を与えられます。 下記の例をご覧ください。

// false を null に変換
$filter = new Zend_Filter_Null(Zend_Filter_Null::BOOLEAN);

// false 及び 0 を null に変換
$filter = new Zend_Filter_Null(
    Zend_Filter_Null::BOOLEAN + Zend_Filter_Null::INTEGER
);

// false 及び 0 を null に変換
$filter = new Zend_Filter_Null( array(
    Zend_Filter_Null::BOOLEAN,
    Zend_Filter_Null::INTEGER
));

// false 及び 0 を null に変換
$filter = new Zend_Filter_Null(array(
    'boolean',
    'integer',
));

希望のタイプをセットするために、 Zend_Config のインスタンスを与えることもできます。 タイプをセットするには、後で setType() を使います。

PregReplace

Zend_Filter_PregReplace performs a search using regular expressions and replaces all found elements.

Supported options for Zend_Filter_PregReplace

The following options are supported for Zend_Filter_PregReplace:

  • match: The pattern which will be searched for.

  • replace: The string which is used as replacement for the matches.

Basic usage

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.

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

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

RealPath(日本語)

このフィルタは与えられたリンクとパス名を解決して、正規化された絶対パス名を返します。

Zend_Filter_RealPath でサポートされるオプション

Zend_Filter_RealPath では、下記のオプションがサポートされます。

  • exists: このオプションの既定値は TRUE で、 与えられたパスが本当に存在するかどうかチェックします。

基本の使い方

与えられたパス名のリンク全てで完全パスが返されます。 '/./' や '/../' への参照、 及び、入力パスの余分な '/' 記号は取り除かれます。 結果のパスにはいかなるシンボリックリンクも無く、 '/./' や '/../' 文字もありません。

たとえばファイルが存在しない場合、 Zend_Filter_RealPathは失敗するとFALSEを返します。 もし最後のパスのコンポーネントだけが存在しない場合、 BSD システムではZend_Filter_RealPathは失敗しますが、 他のシステムではFALSEを返します。

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

// '/www/mypath' を返します。

存在しないパス

それらが存在しないとき、 たとえば、生成したいパスのために実際のパスを取得したいとき、 パスを得るためにしばしば役に立ちます。 初期化でFALSEを渡すこともできますし、 それを設定するためにsetExists()を使うこともできます。

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

// file_exists または realpath が false を返すときでも
// '/www/non/existing/path' を返します。

StringToLower(日本語)

Tこのフィルタは、入力を全て小文字に変換します。

Zend_Filter_StringToLower でサポートされるオプション

Zend_Filter_StringToLower では、下記のオプションがサポートされます。

  • encoding 使用されるべきエンコーディングを設定するためにこのオプションを使用します。

基本的な使用法

これは基本的な例です。

$filter = new Zend_Filter_StringToLower();

print $filter->filter('SAMPLE');
// "sample" を返します

Different encoded strings

デフォルトでは、それはサーバの現時点のロケールで、文字を取り扱うだけです。 他のcharset由来の文字は、無視されます。 さらに、mbstring拡張が環境で利用できるとき、 それらを小文字にすることもできます。 StringToLowerフィルタを初期化するときは、 希望のエンコーディングを設定するだけです。 または、後でエンコーディングを変更するために、 setEncoding()メソッドを使います。

// UTF-8 を使用
$filter = new Zend_Filter_StringToLower('UTF-8');

//または、構成を使うときに、役に立つことがありえる配列を与えます
$filter = new Zend_Filter_StringToLower(array('encoding' => 'UTF-8'));

//または後でこのようにします
$filter->setEncoding('ISO-8859-1');

間違ったエンコーディングの設定

あるエンコーディングを設定したくて、mbstring拡張が環境で利用できないとき、 例外を得ることに注意してください。

また、mbstring拡張でサポートされないエンコーディングを設定しようとしているとき、 例外を得ます。

StringToUpper(日本語)

このフィルタは、入力を全て大文字に変換します。

Zend_Filter_StringToUpper でサポートされるオプション

Zend_Filter_StringToUpper では、下記のオプションがサポートされます。

  • encoding: 使用されるべきエンコーディングを設定するためにこのオプションを使用します。

基本的な使用法

これは、StringToUpper フィルタを使用する基本的な例です。

$filter = new Zend_Filter_StringToUpper();

print $filter->filter('Sample');
// "SAMPLE" を返します

異なるエンコード文字列

StringToLowerフィルタの様に、 このフィルタは、サーバの現時点のロケール由来の文字だけを処理します。 StringToLowerと同様に、 異なる文字セットを使っても動作します。

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

//または後でこのようにします
$filter->setEncoding('ISO-8859-1');

StringTrim

This filter modifies a given string such that certain characters are removed from the beginning and end.

Supported options for Zend_Filter_StringTrim

The following options are supported for Zend_Filter_StringTrim:

  • charlist: List of characters to remove from the beginning and end of the string. If this is not set or is null, the default behavior will be invoked, which is to remove only whitespace from the beginning and end of the string.

Basic usage

A basic example of usage is below:

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

Default behaviour for Zend_Filter_StringTrim

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

StripNewLines(日本語)

このフィルタは与えられた文字列を修正して、その文字列内の改行文字を全て削除します。

Zend_Filter_StripNewLines でサポートされるオプション

Zend_Filter_StripNewLines に対する追加オプションはありません。

基本的な使用法

下記は基本的な使用例です。

$filter = new Zend_Filter_StripNewLines();

print $filter->filter("This is (my)\n\rcontent: ');

上記の例では 'This is (my) content:' を返します。 改行文字が全て削除されたことに注意してください。

StripTags

This filter can strip XML and HTML tags from given content.

Zend_Filter_StripTags is potentially unsecure

Be warned that Zend_Filter_StripTags should only be used to strip all available tags.

Using Zend_Filter_StripTags to make your site secure by stripping some unwanted tags will lead to unsecure and dangerous code.

Zend_Filter_StripTags must not be used to prevent XSS attacks. This filter is no replacement for using Tidy or HtmlPurifier.

Supported options for Zend_Filter_StripTags

The following options are supported for Zend_Filter_StripTags:

  • allowAttribs: This option sets the attributes which are accepted. All other attributes are stripped from the given content

  • allowTags: This option sets the tags which are accepted. All other tags will be stripped from the given content

Basic usage

See the following example for the default behaviour of this filter:

$filter = new Zend_Filter_StripTags();

print $filter->filter('<B>My content</B>');

As result you will get the stripped content 'My content'.

When the content contains broken or partitial tags then the complete following content will be erased. See the following example:

$filter = new Zend_Filter_StripTags();

print $filter->filter('This contains <a href="http://example.com">no ending tag');

The above will return 'This contains' with the rest being stripped.

Allowing defined tags

Zend_Filter_StripTags allows stripping of all but defined tags. This can be used for example to strip all tags but links from a text.

$filter = new Zend_Filter_StripTags(array('allowTags' => 'a'));

$input  = "A text with <br/> a <a href='link.com'>link</a>";
print $filter->filter($input);
// returns: A text with a <a href='link.com'>link</a>

The above example strips all tags but the link. By providing an array you can set multiple tags at once.

警告

Do not use this feature to get a probably secure content. This component does not replace the use of a proper configured html filter.

Allowing defined attributes

It is also possible to strip all but allowed attributes from a tag.

$filter = new Zend_Filter_StripTags(array('allowAttribs' => 'src'));

$input  = "A text with <br/> a <img src='picture.com' width='100'>picture</img>";
print $filter->filter($input);
// returns: A text with a <img src='picture.com'>picture</img>

The above example strips all tags but img. Additionally from the img tag all attributes but src will be stripped. By providing an array you can set multiple attributes at once.