標準のフィルタクラス群Zend Framework には、すぐに使える標準のフィルタ群が同梱されています。 Alnum(日本語)Zend_Filter_Alnum は、 アルファベットおよび数字だけを返すフィルターです。 他の全ての文字は抑止されます。 (訳注:ここでいうアルファベットおよび数字、とは各言語で使われる文字および数字の集合を意味します。英数字ではありません) Zend_Filter_Alnum でサポートされるオプション下記のオプションが Zend_Filter_Alnum でサポートされます。
基本的な使用法このフィルターの既定の振る舞いについては、下記の例をご覧ください。
上記の例は 'Thisismycontent123' を返します。 ご覧の通り、空白の全てと、括弧がフィルターされています。
空白を許すZend_Filter_Alnum は空白も許します。 テキストから特別な文字を取り除きたい場合に役立つでしょう。下記の例をご覧ください。
上記の例は 'This is my content 123' を返します。 ご覧の通り、括弧はフィルターされましたが、空白には手が出されていません。 あとで allowWhiteSpace を変更するには、 setAllowWhiteSpace() や getAllowWhiteSpace() を使用できます。 Alpha(日本語)Zend_Filter_Alpha は、 アルファベットを残して全てを削除した、文字列 $value を返すフィルターです。このフィルターには、空白文字を許すオプションもあります。 (訳注:ここでいうアルファベット、とはアラビア文字やタイ文字、キリール文字など、各言語で使われる文字の集合を意味します。英字とは限りません) Zend_Filter_Alpha でサポートされるオプション下記のオプションが Zend_Filter_Alpha でサポートされます。
基本的な使用法下記は基本的な使用例です。
上記の例は 'Thisismycontent' を返します。 空白文字と括弧が削除されていることに注意してください。
空白文字を許すZend_Filter_Alpha は空白文字も許せます。 文字列から特別な文字を取り除きたい場合に役立つでしょう。 下記の例をご覧ください。 上記の例は 'This is my content ' を返します。 括弧やコロン、数字が全て除去された一方で、空白文字が残っていることに注意してください。 メソッド初期化後に allowWhiteSpace を変更するには、 setAllowWhiteSpace() が使えるでしょう。 メソッドの現在の allowWhiteSpace の値を照会するには、 getAllowWhiteSpace() が使えるでしょう。 BaseName(日本語)Zend_Filter_BaseName により、 ファイルへのパスを含む文字列をフィルタできます。 このファイルの基盤名を返します。 Zend_Filter_BaseName でサポートされるオプションZend_Filter_BaseName に対する追加オプションはありません。 BooleanThis 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_BooleanThe following options are supported for Zend_Filter_Boolean:
Default behaviour for Zend_Filter_BooleanBy default, this filter works by casting the input to a BOOLEAN value; in other words, it operates in a similar fashion to calling (boolean) $value.
This means that without providing any configuration, Zend_Filter_Boolean accepts all input types and returns a BOOLEAN just as you would get by type casting to BOOLEAN. Changing behaviour for Zend_Filter_BooleanSometimes casting with (boolean) will not suffice. Zend_Filter_Boolean allows you to configure specific types to convert, as well as which to omit. The following types can be handled:
All other given values will return TRUE by default. There are several ways to select which of the above types are filtered. You can give one or multiple types and add them, you can give an array, you can use constants, or you can give a textual string. See the following examples:
You can also give an instance of Zend_Config to set the desired types. To set types after instantiation, use the setType() method. Localized booleansAs mentioned previously, Zend_Filter_Boolean can also recognise localized "yes" and "no" strings. This means that you can ask your customer in a form for "yes" or "no" within his native language and Zend_Filter_Boolean will convert the response to the appropriate boolean value. To set the desired locale, you can either use the locale option, or the method setLocale(). Disable castingSometimes it is necessary to recognise only TRUE or FALSE and return all other values without changes. Zend_Filter_Boolean allows you to do this by setting the casting option to FALSE. In this case Zend_Filter_Boolean will work as described in the following table, which shows which values return TRUE or FALSE. All other given values are returned without change when casting is set to FALSE
The following example shows the behaviour when changing the casting option: Callback(日本語)このフィルタにより、 Zend_Filter とともに自分自身のメソッドを使えます。 機能を果たすメソッドがすでにあるとき、新しいフィルタを生成する必要はありません。 Zend_Filter_Callback でサポートされるオプションZend_Filter_Callback では、下記のオプションがサポートされます。
基本的な使用法このフィルタの使い方はとても簡単です。 文字列を逆にするフィルタを生成したいとしましょう。
おわかりのように、自分自身のフィルタを定義するために本当に簡単にコールバックを使えます。 メソッド(それはクラス内で定義されます)をコールバックとして配列を与えることによって使うこともできます。 実際に設定されているコールバックを取得するには getCallback() を使い、 他のコールバックを設定するには setCallback() を使います。
Default parameters within a callbackフィルタが実行されるとき、 呼ばれるメソッドに配列として与えられるデフォルト・パラメータを定義できます。 この配列は、フィルターされた値で結合されます。 手動で上記のメソッド定義を呼ぶと、それはこのように見えます:
Compress and DecompressThese two filters are capable of compressing and decompressing strings, files, and directories. Supported options for Zend_Filter_Compress and Zend_Filter_DecompressThe following options are supported for Zend_Filter_Compress and Zend_Filter_Decompress:
Supported compression adaptersThe 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) Generic handlingTo create a compression filter you need to select the compression format you want to use. The following description takes the Bz2 adapter. Details for all other adapters are described after this section. The two filters are basically identical, in that they utilize the same backends. Zend_Filter_Compress should be used when you wish to compress items, and Zend_Filter_Decompress should be used when you wish to decompress items. For instance, if we want to compress a string, we have to initiate Zend_Filter_Compress and indicate the desired adapter.
To use a different adapter, you simply specify it to the constructor. You may also provide an array of options or Zend_Config object. If you do, provide minimally the key "adapter", and then either the key "options" or "adapterOptions" (which should be an array of options to provide to the adapter on instantiation).
Almost the same usage is we want to decompress a string. We just have to use the decompression filter in this case.
To get the compressed string, we have to give the original string. The filtered value is the compressed version of the original string.
Decompression works the same way.
Creating an archiveCreating an archive file works almost the same as compressing a string. However, in this case we need an additional parameter which holds the name of the archive we want to create. In the above example the uncompressed string is compressed, and is then written into the given archive file.
When you want to compress a file, then you must give the name of the file with its path. You may also specify a directory instead of a filename. In this case the whole directory with all its files and subdirectories will be compressed into the archive.
Decompressing an archiveDecompressing an archive file works almost like compressing it. You must specify either the archive parameter, or give the filename of the archive when you decompress the file.
Some adapters support decompressing the archive into another subdirectory. In this case you can set the target parameter.
Bz2 AdapterThe Bz2 Adapter can compress and decompress:
This adapter makes use of PHP's Bz2 extension. To customize compression, this adapter supports the following options:
All options can be set at instantiation or by using a related method. For example, the related methods for 'Blocksize' are getBlocksize() and setBlocksize(). You can also use the setOptions() method which accepts all options as array. Gz AdapterThe Gz Adapter can compress and decompress:
This adapter makes use of PHP's Zlib extension. To customize the compression this adapter supports the following options:
All options can be set at initiation or by using a related method. For example, the related methods for 'Level' are getLevel() and setLevel(). You can also use the setOptions() method which accepts all options as array. Lzf AdapterThe Lzf Adapter can compress and decompress:
This adapter makes use of PHP's Lzf extension. There are no options available to customize this adapter. Rar AdapterThe Rar Adapter can compress and decompress:
This adapter makes use of PHP's Rar extension.
To customize the compression this adapter supports the following options:
All options can be set at instantiation or by using a related method. For example, the related methods for 'Target' are getTarget() and setTarget(). You can also use the setOptions() method which accepts all options as array. Tar AdapterThe Tar Adapter can compress and decompress:
This adapter makes use of PEAR's Archive_Tar component. To customize the compression this adapter supports the following options:
All options can be set at instantiation or by using a related method. For example, the related methods for 'Target' are getTarget() and setTarget(). You can also use the setOptions() method which accepts all options as array.
Zip AdapterThe Zip Adapter can compress and decompress:
This adapter makes use of PHP's Zip extension. To customize the compression this adapter supports the following options:
All options can be set at instantiation or by using a related method. For example, the related methods for 'Target' are getTarget() and setTarget(). You can also use the setOptions() method which accepts all options as array. 数字文字列 $value を返します。数字以外の全てを除去します。 Zend_Filter_Digits でサポートされるオプションZend_Filter_Digits に対する追加オプションはありません。 Dir(日本語)ファイルへのパスを含む文字列が与えられると、この関数は、ディレクトリ名を返します。 Zend_Filter_Dir でサポートされるオプションZend_Filter_Dir に対する追加オプションはありません。 Encrypt 及び Decryptこれらのフィルタにより、与えられた文字列を暗号化したり復号化できます。 したがって、それらはアダプタを利用します。 実際には、PHP 由来の Mcrypt および OpenSSL エクステンション用のアダプタを使用します。 Zend_Filter_Encrypt および Zend_Filter_Decrypt でサポートされるオプションZend_Filter_Encrypt および Zend_Filter_Decrypt では、下記のオプションがサポートされます。
アダプタの使用法これら 2 つの暗号化手法はまったく異なる方式なので、 アダプタの使用法もそれぞれ異なります。 フィルタを初期化する際に、どのアダプタを使うかを選ばなければなりません。 別のアダプタを設定するために setAdapter() を使用することもできます。また getAdapter() メソッドで、実際に設定されているアダプタを取得できます。
Mcrypt での暗号化Mcrypt 拡張モジュールをインストールすると、 Mcrypt アダプタが使えるようになります。 もしオプション配列の代わりに文字列を与えると、 文字列がキーとして使われます。 初期化した後で暗号化の値を取得したり設定したりするには、それぞれ getEncryption() および setEncryption() メソッドを使用します。
暗号化ベクトルの取得や設定には、それぞれ getVector() および setVector() を使用可能です。指定した文字列が、 そのアルゴリズムに必要なベクトルのサイズに応じて切り詰められたり伸ばされたりします。
Mcrypt の復号Mcrypt で暗号化したコンテンツを復号するには、 暗号化を行った際に指定したオプションが必要です。 ここでひとつ暗号化時との大きな違いがあります。 暗号化の際にベクトルを指定しなかった場合は、コンテンツを暗号化した後で フィルタの getVector() メソッドを使用してベクトルを取得する必要があります。 正しいベクトルがなければ、コンテンツを復号することはできません。 オプションを指定しさえすれば、復号は暗号化と同じくらい単純なことです。
OpenSSL での暗号化OpenSSL 拡張モジュールをインストールすると、 OpenSSL アダプタが使えるようになります。 後から公開鍵を取得あるいは設定するには、 getPublicKey() および setPublicKey() メソッドを使用します。 秘密鍵についても、 getPrivateKey() および setPrivateKey() メソッドで取得あるいは設定できます。
鍵自体も暗号化したい場合は、パスフレーズを setPassphrase() メソッドで渡します。 パスフレーズつきで暗号化したコンテンツを復号したい場合は、 公開鍵だけではなく (暗号化された鍵を復号するための) パスフレーズも必要となります。 最後に、OpenSSL を使用した場合に受け手に渡す必要があるものをまとめます。 暗号化されたコンテンツ、パスフレーズを使用した場合はそのパスフレーズ、 そして復号用のエンベロープ鍵。これらが必要となります。 つまり、暗号化を終えたらエンベロープ鍵を取得する必要があるということです。 取得するには getEnvelopeKey() メソッドを使用します。 OpenSSL でコンテンツの暗号化を行う完全な例は、このようになります。
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. 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.
OpenSSL での復号OpenSSL での復号は、暗号化と同様にシンプルです。 しかし、コンテンツを暗号化した人からすべてのデータを受け取る必要があります。 下記の例をご覧ください。
オプションで、パスフレーズを渡さなければ鍵を復号できないようにすることもできます。 そのために使用するのが setPassphrase() メソッドです。
最後に、コンテンツを復号します。 暗号化したコンテンツの復号を行う完全な例は、このようになります。
HtmlEntitiesReturns the string $value, converting characters to their corresponding HTML entity equivalents where they exist. Supported options for Zend_Filter_HtmlEntitiesThe following options are supported for Zend_Filter_HtmlEntities:
Basic usageSee the following example for the default behaviour of this filter.
Quote StyleZend_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: The above example returns A 'single' and "double". Notice that 'single' as well as "double" quotes are filtered. The above example returns A 'single' and "double". Notice that "double" quotes are filtered while 'single' quotes are not altered. The above example returns A 'single' and "double". Notice that neither "double" or 'single' quotes are altered. Helper MethodsTo 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
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.
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.
Int(日本語)Zend_Filter_Int により、含んでいるスカラー値を整数に変換できます Zend_Filter_Int でサポートされるオプションZend_Filter_Int に対する追加オプションはありません。 基本的な使用法下記は基本的な使用例です。
これは '-4' を返します。 LocalizedToNormalized および NormalizedToLocalizedThese 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 では、下記のオプションがサポートされます。
ワークフロー正規化により、ユーザーがユーザー固有の言語表記で情報を入力でき、 さらに例えば正規化された値をデータベースに保存できるようになります。 一方で地域化により、 保管された正規化された値をローカルな流儀でユーザーに提示できるようになります。
下記の入力型が正規化および地域化されます。
その他の入力はいずれも変更無しにそのまま返されます。
数値の正規化整数、フロートまたは実数のようなあらゆる数が正規化されます。 指数表記の数値は、実はこのフィルタで扱えないので注意してください。 数値についての詳しい正規化方法
アプリケーション全体のロケールとしてロケール 'de' を設定したつもりになりましょう。 Zend_Filter_LocalizedToNormalizedは設定されたロケールを受け取って、 どの種類の入力をあなたが与えたか検出するために、それを使います。 われわれの例ではそれは精度を持つ値でした。 そこで、この値を文字列として正規化した表現をフィルタは返します。 正規化した数がどのようになるべきか、コントロールすることもできます。 このためにZend_Locale_Formatでも使用されるオプションを 全て与えられます。利用可能なオプションについて詳しくは、この Zend_Locale の章をご覧ください。 下記はオプションの動作方法が分かるように精度を定義した例です。
日時の正規化日付や時刻の値の入力もまた、正規化できます。 与えられた日付および時刻は全て、独自のキーの範囲内で日付の各部分が与えられた 配列として返されます。
ふたたびロケール 'de' を設定したつもりになりましょう。 そこで、入力は自動的に日付として検出され、 返り値に連想配列を受け取ります。 もちろん、日付の入力値をどのようにするか date_formatやlocaleオプションで コントロールすることもできます。
数値の地域化整数、フロートまたは実数のようなあらゆる数が地域化されます。 指数表記の数値は、実はこのフィルタで扱えないので注意してください。 数値についての詳しい地域化方法
アプリケーション全体のロケールとしてロケール 'de' を設定したつもりになりましょう。 Zend_Filter_NormalizedToLocalizedは設定されたロケールを受け取って、 どの種類の出力をあなたが受け取りたいのか検出するために、それを使います。 われわれの例ではそれは精度を持つ値でした。 そこで、この値を文字列として地域化した表現をフィルタは返します。 地域化した数がどのようになるべきか、コントロールすることもできます。 このためにZend_Locale_Formatでも使用されるオプションを 全て与えられます。 これらのオプションの使い方について詳しくは、この Zend_Locale の章 をご覧ください。 下記はオプションの動作方法が分かるように精度を定義した例です。
日時の地域化日付や時刻の値を正規化したものもまた、地域化できます。 与えられた日付および時刻は全て、設定されたロケールで定義された形式で 文字列として返されます。
ふたたびロケール 'de' を設定したつもりになりましょう。 そこで、入力は自動的に日付として検出され、 ロケール 'de' で定義された形式で返されます。 もちろん、日付の入力値をどのようにするか date_formatやlocaleオプションで コントロールすることもできます。 Null(日本語)もし指定の基準を満たす場合、 このフィルタは与えられた入力を NULLに変更します。 データベースとともに作業するとき、これはしばしば必要です そして、ブールや他のあらゆるタイプの代わりに NULL値を持つことを望みます。 Zend_Filter_Null でサポートされるオプションZend_Filter_Null では、下記のオプションがサポートされます。
Zend_Filter_Null の既定の振る舞いデフォルトではこのフィルタはPHPの empty()メソッドのように動作します。 言い換えると、もし empty()がブールの TRUE を返す場合、 NULL値が返されます。
これは、何の設定もなしに、 Zend_Filter_Nullは全ての入力を受け付けて、 empty()と同じ場合に NULLを返すことを意味します。 他の値ではいずれも変更無しにそのまま返されます。 Zend_Filter_Null の振る舞いの変更しばしば、 empty()に基づくフィルタでは十分ではありません。 そのため、Zend_Filter_Nullでは、 いずれのタイプが変換されるか否か設定できます。 下記のタイプが操作できます。
上記のタイプのうちのいずれをフィルタするかを選択する方法が、いくつかあります。 1つまたは複数のタイプを与えて、それに追加できます。 配列を与えたり、定数を使えます。 または、原文の文字列を与えられます。 下記の例をご覧ください。
希望のタイプをセットするために、 Zend_Config のインスタンスを与えることもできます。 タイプをセットするには、後で setType() を使います。 PregReplaceZend_Filter_PregReplace performs a search using regular expressions and replaces all found elements. Supported options for Zend_Filter_PregReplaceThe following options are supported for Zend_Filter_PregReplace:
Basic usageTo 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.
You can use getMatchPattern() and setMatchPattern() to set the matching pattern afterwards. To set the replacement pattern you can use getReplacement() and setReplacement(). For a more complex usage take a look into PHP's » PCRE Pattern Chapter. RealPath(日本語)このフィルタは与えられたリンクとパス名を解決して、正規化された絶対パス名を返します。 Zend_Filter_RealPath でサポートされるオプションZend_Filter_RealPath では、下記のオプションがサポートされます。
基本の使い方与えられたパス名のリンク全てで完全パスが返されます。 '/./' や '/../' への参照、 及び、入力パスの余分な '/' 記号は取り除かれます。 結果のパスにはいかなるシンボリックリンクも無く、 '/./' や '/../' 文字もありません。 たとえばファイルが存在しない場合、 Zend_Filter_RealPathは失敗するとFALSEを返します。 もし最後のパスのコンポーネントだけが存在しない場合、 BSD システムではZend_Filter_RealPathは失敗しますが、 他のシステムではFALSEを返します。
存在しないパスそれらが存在しないとき、 たとえば、生成したいパスのために実際のパスを取得したいとき、 パスを得るためにしばしば役に立ちます。 初期化でFALSEを渡すこともできますし、 それを設定するために setExists()を使うこともできます。
StringToLower(日本語)Tこのフィルタは、入力を全て小文字に変換します。 Zend_Filter_StringToLower でサポートされるオプションZend_Filter_StringToLower では、下記のオプションがサポートされます。
基本的な使用法これは基本的な例です。
Different encoded stringsデフォルトでは、それはサーバの現時点のロケールで、文字を取り扱うだけです。 他のcharset由来の文字は、無視されます。 さらに、mbstring拡張が環境で利用できるとき、 それらを小文字にすることもできます。 StringToLowerフィルタを初期化するときは、 希望のエンコーディングを設定するだけです。 または、後でエンコーディングを変更するために、 setEncoding()メソッドを使います。
StringToUpper(日本語)このフィルタは、入力を全て大文字に変換します。 Zend_Filter_StringToUpper でサポートされるオプションZend_Filter_StringToUpper では、下記のオプションがサポートされます。
基本的な使用法これは、StringToUpper フィルタを使用する基本的な例です。
異なるエンコード文字列StringToLowerフィルタの様に、 このフィルタは、サーバの現時点のロケール由来の文字だけを処理します。 StringToLowerと同様に、 異なる文字セットを使っても動作します。
StringTrimThis filter modifies a given string such that certain characters are removed from the beginning and end. Supported options for Zend_Filter_StringTrimThe following options are supported for Zend_Filter_StringTrim:
Basic usageA basic example of usage is below:
The above example returns 'This is (my) content:'. Notice that the whitespace characters have been removed. Default behaviour for Zend_Filter_StringTrim
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 に対する追加オプションはありません。 基本的な使用法下記は基本的な使用例です。
上記の例では 'This is (my) content:' を返します。 改行文字が全て削除されたことに注意してください。 StripTagsThis filter can strip XML and HTML tags from given content. Warning
Zend_Filter_StripTags is potentially unsecureBe 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_StripTagsThe following options are supported for Zend_Filter_StripTags:
Basic usageSee the following example for the default behaviour of this filter:
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:
The above will return 'This contains' with the rest being stripped. Allowing defined tagsZend_Filter_StripTags allows stripping of all but defined tags. This can be used for example to strip all tags but links from a text. The above example strips all tags but the link. By providing an array you can set multiple tags at once. Warning
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 attributesIt is also possible to strip all but allowed attributes from a tag. 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.
|