本文實(shí)例講述了zend framework過(guò)濾器zend_filter用法。分享給大家供大家參考,具體如下:
引言:過(guò)濾器是對(duì)輸入內(nèi)容進(jìn)行過(guò)濾,清除其中不符合過(guò)濾規(guī)則的內(nèi)容,并將其余內(nèi)容返回的過(guò)程。
Zend中有個(gè)Zend_Filter組件用來(lái)實(shí)現(xiàn)過(guò)濾的功能。其中有個(gè)Zend_Filter_Interface子類(lèi),該子類(lèi)為實(shí)現(xiàn)一般過(guò)濾器提供了接口。
要實(shí)現(xiàn)過(guò)濾器類(lèi),需要實(shí)現(xiàn)該接口中一個(gè)名為filter()的方法。
下面通過(guò)實(shí)例來(lái)演示如何使用Zend_Filter中定義的過(guò)濾器,該例演示如何實(shí)現(xiàn)字母轉(zhuǎn)小寫(xiě)的功能。
代碼:
<?php require_once 'Zend/Filter/StringToLower.php'; //加載子類(lèi) $filter = new Zend_Filter_StringToLower; //實(shí)例化對(duì)象 $temp1 = "ABCDefGH"; //定義待過(guò)濾內(nèi)容 $temp2 = "我愛(ài)Nan Jing"; echo "內(nèi)容:".$temp1."<p>經(jīng)過(guò)濾后為:"; echo $filter->filter($temp1); echo "<p>"; echo "內(nèi)容:".$temp2."<p>經(jīng)過(guò)濾后為:"; echo $filter->filter($temp2);
? ?
結(jié)果:
內(nèi)容:ABCDefGH
經(jīng)過(guò)濾后為:abcdefgh
內(nèi)容:我愛(ài)Nan Jing
經(jīng)過(guò)濾后為:我愛(ài)nan jing
為什么如此神奇呢?不禁讓我想探索一下其內(nèi)部的構(gòu)造!下面來(lái)研讀一下其內(nèi)部的工作原理。
class Zend_Filter_StringToLower implements Zend_Filter_Interface { /** * Encoding for the input string * * @var string */ protected $_encoding = null; /** * Constructor * * @param string|array|Zend_Config $options OPTIONAL */ public function __construct($options = null) { if ($options instanceof Zend_Config) { $options = $options->toArray(); } else if (!is_array($options)) { $options = func_get_args(); $temp = array(); if (!empty($options)) { $temp['encoding'] = array_shift($options); } $options = $temp; } if (!array_key_exists('encoding', $options) && function_exists('mb_internal_encoding')) { $options['encoding'] = mb_internal_encoding(); } if (array_key_exists('encoding', $options)) { $this->setEncoding($options['encoding']); } } /** * Returns the set encoding * * @return string */ public function getEncoding() { return $this->_encoding; } /** * Set the input encoding for the given string * * @param string $encoding * @return Zend_Filter_StringToLower Provides a fluent interface * @throws Zend_Filter_Exception */ public function setEncoding($encoding = null) { if ($encoding !== null) { if (!function_exists('mb_strtolower')) { require_once 'Zend/Filter/Exception.php'; throw new Zend_Filter_Exception('mbstring is required for this feature'); } $encoding = (string) $encoding; if (!in_array(strtolower($encoding), array_map('strtolower', mb_list_encodings()))) { require_once 'Zend/Filter/Exception.php'; throw new Zend_Filter_Exception("The given encoding '$encoding' is not supported by mbstring"); } } $this->_encoding = $encoding; return $this; } /** * Defined by Zend_Filter_Interface * * Returns the string $value, converting characters to lowercase as necessary * * @param string $value * @return string */ public function filter($value) { if ($this->_encoding !== null) { return mb_strtolower((string) $value, $this->_encoding); } return strtolower((string) $value); } }
? ?
研讀:
源代碼意思大概是先實(shí)現(xiàn)Zend_Filter_Interface接口。
定義一個(gè)私有變量$_encoding,初始值為null,一般私有變量都是以_下劃線開(kāi)頭。
然后通過(guò)構(gòu)造函數(shù)進(jìn)行初始化工作,設(shè)置encoding。
至于這個(gè)encoing屬性是作何用的,我就不大清楚了,反正為了它,源碼寫(xiě)了不少代碼。
類(lèi)中有三個(gè)方法,一個(gè)是setEncoding,一個(gè)是getEncoding,一個(gè)主要功能的filter。有兩個(gè)方法都是為了encoding來(lái)寫(xiě)的。
在構(gòu)造函數(shù)中使用setEncoding方法直接用$this->setEncoding()就可。就可以把私有屬性設(shè)置好值了。
然后根據(jù)私有屬性的內(nèi)容來(lái)選擇使用什么方法來(lái)使得字母變小寫(xiě)。
我去,這個(gè)類(lèi)考慮的東西還真夠多的。其實(shí)核心代碼就那兩句,strtolower((string) $value)。
這個(gè)類(lèi)很酷,我從來(lái)沒(méi)用過(guò)私有屬性??紤]問(wèn)題也沒(méi)有作者那么全面,各種驗(yàn)證,各種情況考慮。比如,
從構(gòu)造函數(shù)中就可以看出他考慮問(wèn)題的全面性。
if ($options instanceof Zend_Config) { $options = $options->toArray(); } else if (!is_array($options)) { $options = func_get_args(); $temp = array(); if (!empty($options)) { $temp['encoding'] = array_shift($options); } $options = $temp; } if (!array_key_exists('encoding', $options) && function_exists('mb_internal_encoding')) { $options['encoding'] = mb_internal_encoding(); } if (array_key_exists('encoding', $options)) { $this->setEncoding($options['encoding']); }
? ?
總的來(lái)說(shuō)還是值得佩服的。
下面談?wù)勥^(guò)濾器鏈,它的作用是將多個(gè)過(guò)濾器串聯(lián)起來(lái)配合使用。過(guò)濾器鏈就是多個(gè)過(guò)濾器的一個(gè)連接。在對(duì)指定的內(nèi)容進(jìn)行過(guò)濾時(shí),
每個(gè)過(guò)濾器將按照其順序分別進(jìn)行過(guò)濾或者轉(zhuǎn)化操作。當(dāng)所有的過(guò)濾操作都執(zhí)行完畢時(shí),過(guò)濾器鏈返回最終的過(guò)濾結(jié)果。
聽(tīng)起來(lái)蠻有趣的??!
具體實(shí)現(xiàn)步驟是什么呢?
首先要為類(lèi)Zend_Filter實(shí)例化一個(gè)對(duì)象,然后通過(guò)該實(shí)例的addFilter()方法向過(guò)濾器鏈中添加過(guò)濾器。
下面通過(guò)示例演示如何使用過(guò)濾器鏈對(duì)數(shù)據(jù)進(jìn)行多重過(guò)濾及轉(zhuǎn)化。
代碼:
<?php require_once 'Zend/Filter.php'; //加載Zend_Filter類(lèi) require_once 'Zend/Filter/Alpha.php'; //加載Zend_Filter_Alpha子類(lèi) require_once 'Zend/Filter/StringToUpper.php'; //加載Zend_Filter_StringToUpper子類(lèi) $filterChain = new Zend_Filter(); //創(chuàng)建過(guò)濾器鏈 $filterChain ->addFilter(new Zend_Filter_Alpha(" ")) ->addFilter(new Zend_Filter_StringToUpper());//向過(guò)濾器鏈中添加過(guò)濾器 $temp1 = "12345asdf67asdfasdf"; $temp2 = "#$%^!@fffff"; $temp3 = "Welcome to Bei Jing"; echo "內(nèi)容:".$temp1."<p>經(jīng)過(guò)過(guò)濾后為:"; echo $filterChain->filter($temp1); echo "<p>"; echo "內(nèi)容:".$temp2."<p>經(jīng)過(guò)過(guò)濾后為:"; echo $filterChain->filter($temp2); echo "<p>"; echo "內(nèi)容:".$temp3."<p>經(jīng)過(guò)過(guò)濾后為:"; echo $filterChain->filter($temp3); echo "<p>";
? ?
結(jié)果:
內(nèi)容:12345asdf67asdfasdf
經(jīng)過(guò)過(guò)濾后為:ASDFASDFASDF
內(nèi)容:#$%^!@fffff
經(jīng)過(guò)過(guò)濾后為:FFFFF
內(nèi)容:Welcome to Bei Jing
經(jīng)過(guò)過(guò)濾后為:WELCOME TO BEI JING
分析:
這里的Alpha很強(qiáng)大啊,過(guò)濾數(shù)字和特殊字符,連空格都能過(guò)濾。還好我初始化的時(shí)候加了個(gè)參數(shù)" ",才使得空格保留了下來(lái)。
為何如此神奇呢?
核心代碼就這一塊
public function filter($value) { $whiteSpace = $this->allowWhiteSpace ? '\s' : ''; if (!self::$_unicodeEnabled) { // POSIX named classes are not supported, use alternative a-zA-Z match $pattern = '/[^a-zA-Z' . $whiteSpace . ']/'; } else if (self::$_meansEnglishAlphabet) { //The Alphabet means english alphabet. $pattern = '/[^a-zA-Z' . $whiteSpace . ']/u'; } else { //The Alphabet means each language's alphabet. $pattern = '/[^\p{L}' . $whiteSpace . ']/u'; } return preg_replace($pattern, '', (string) $value); }
? ?
分析:這里對(duì)內(nèi)容進(jìn)行過(guò)濾,如果不是字母或者空格,就統(tǒng)統(tǒng)去掉。用到的php方法是preg_replace。此外,還用到了正則表達(dá)式。[^a-zA-Z]表示除此之外的其他字符。
這里的$whiteSpace成員屬性,是初始化的時(shí)候設(shè)置的,具體代碼如下:
public function __construct($allowWhiteSpace = false) { if ($allowWhiteSpace instanceof Zend_Config) { $allowWhiteSpace = $allowWhiteSpace->toArray(); } else if (is_array($allowWhiteSpace)) { if (array_key_exists('allowwhitespace', $allowWhiteSpace)) { $allowWhiteSpace = $allowWhiteSpace['allowwhitespace']; } else { $allowWhiteSpace = false; } } $this->allowWhiteSpace = (boolean) $allowWhiteSpace; if (null === self::$_unicodeEnabled) { self::$_unicodeEnabled = (@preg_match('/\pL/u', 'a')) ? true : false; } if (null === self::$_meansEnglishAlphabet) { $this->_locale = new Zend_Locale('auto'); self::$_meansEnglishAlphabet = in_array($this->_locale->getLanguage(), array('ja', 'ko', 'zh') ); } }
? ?
此外,還有兩個(gè)方法來(lái)設(shè)置是否允許有空格和獲取是否設(shè)置了允許空格。
/** * Returns the allowWhiteSpace option * * @return boolean */ public function getAllowWhiteSpace() { return $this->allowWhiteSpace; } /** * Sets the allowWhiteSpace option * * @param boolean $allowWhiteSpace * @return Zend_Filter_Alpha Provides a fluent interface */ public function setAllowWhiteSpace($allowWhiteSpace) { $this->allowWhiteSpace = (boolean) $allowWhiteSpace; return $this; }
? ?
剖析完之后,我們似乎就更了解它的構(gòu)造了,就是使用正則過(guò)濾而已。同時(shí)通過(guò)屬性allowWhiteSpace來(lái)控制是否過(guò)濾空格。
剛才介紹了兩種過(guò)濾器,一個(gè)是StringToUpper,一個(gè)是Alpha,下面再介紹其它的一些過(guò)濾器。
首先是Alnum,過(guò)濾非數(shù)字和非字母的內(nèi)容,執(zhí)行filter()方法,將返回純數(shù)字與字母的內(nèi)容,它是Zend_Filter_Alpha(過(guò)濾非字母)與Zend_Filter_Digits(過(guò)濾非數(shù)值)的并集。
具體的例子就不舉了,都差不多。
我們來(lái)看看它內(nèi)部的構(gòu)造,
public function filter($value) { $whiteSpace = $this->allowWhiteSpace ? '\s' : ''; if (!self::$_unicodeEnabled) { // POSIX named classes are not supported, use alternative a-zA-Z0-9 match $pattern = '/[^a-zA-Z0-9' . $whiteSpace . ']/'; } else if (self::$_meansEnglishAlphabet) { //The Alphabet means english alphabet. $pattern = '/[^a-zA-Z0-9' . $whiteSpace . ']/u'; } else { //The Alphabet means each language's alphabet. $pattern = '/[^\p{L}\p{N}' . $whiteSpace . ']/u'; } return preg_replace($pattern, '', (string) $value); }
? ?
通過(guò)正則過(guò)濾除字母和數(shù)字之外的內(nèi)容。
下面出場(chǎng)的是HtmlEntities HTML過(guò)濾器。
代碼:
<?php require_once 'Zend/Filter/Htmlentities.php'; $filter = new Zend_Filter_HtmlEntities(); $temp1 = "@@##@@"; $temp2 = "<button>aaa</button>"; $temp3 = "<h1>Welcome to Bei Jing</h1>"; echo "內(nèi)容:".$temp1."<p>經(jīng)過(guò)過(guò)濾為:"; echo $filter->filter($temp1); echo "<p>"; echo "內(nèi)容:".$temp2."<p>經(jīng)過(guò)過(guò)濾為:"; echo $filter->filter($temp2); echo "<p>"; echo "內(nèi)容:".$temp3."<p>經(jīng)過(guò)過(guò)濾為:"; echo $filter->filter($temp3); echo "<p>";
? ?
結(jié)果:
通過(guò)結(jié)果,我們看出它將html內(nèi)容還原成原始代碼了。由于該過(guò)濾器是對(duì)函數(shù)htmlentities進(jìn)行的封裝,所以遵循該函數(shù)的規(guī)則。即將“”分別轉(zhuǎn)換為“”,經(jīng)過(guò)這樣的轉(zhuǎn)換,
相應(yīng)的HTML內(nèi)容就變成了以其原始格式顯示的字符串。
希望本文所述對(duì)大家基于Zend Framework框架的PHP程序設(shè)計(jì)有所幫助。
更多Zend Framework過(guò)濾器Zend_Filter用法詳解相關(guān)文章請(qǐng)關(guān)注PHP中文網(wǎng)!
每個(gè)人都需要一臺(tái)速度更快、更穩(wěn)定的 PC。隨著時(shí)間的推移,垃圾文件、舊注冊(cè)表數(shù)據(jù)和不必要的后臺(tái)進(jìn)程會(huì)占用資源并降低性能。幸運(yùn)的是,許多工具可以讓 Windows 保持平穩(wěn)運(yùn)行。
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://www.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)