本文實(shí)例講述了Zend Framework過濾器Zend_Filter用法。分享給大家供大家參考,具體如下:
引言:過濾器是對輸入內(nèi)容進(jìn)行過濾,清除其中不符合過濾規(guī)則的內(nèi)容,並將其餘內(nèi)容返回的過程。
Zend中有個(gè)Zend_Filter元件用來實(shí)現(xiàn)過濾的功能。其中有個(gè)Zend_Filter_Interface子類,該子類為實(shí)作一般過濾器提供了介面。
要實(shí)作過濾器類,需要實(shí)作該介面中一個(gè)名為filter()的方法。
下面透過實(shí)例來示範(fàn)如何使用Zend_Filter中定義的篩選器,該範(fàn)例示範(fàn)如何實(shí)作字母轉(zhuǎn)小寫的功能。
代碼:
<?php require_once 'Zend/Filter/StringToLower.php'; //加載子類 $filter = new Zend_Filter_StringToLower; //實(shí)例化對象 $temp1 = "ABCDefGH"; //定義待過濾內(nèi)容 $temp2 = "我愛Nan Jing"; echo "內(nèi)容:".$temp1."<p>經(jīng)過濾后為:"; echo $filter->filter($temp1); echo "<p>"; echo "內(nèi)容:".$temp2."<p>經(jīng)過濾后為:"; echo $filter->filter($temp2);
? ?
結(jié)果:
內(nèi)容:ABCDefGH
過濾後為:abcdefgh
內(nèi)容:我愛Nan Jnan Jan J30303000ing不禁讓我想探索一下其內(nèi)部的構(gòu)造!下面來研讀一下其內(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í)作Zend_Filter_Interface介面。 定義一個(gè)私有變數(shù)$_encoding,初始值為null,一般私有變數(shù)都是以_底線開頭。 然後透過建構(gòu)函式進(jìn)行初始化工作,設(shè)定encoding。 至於這個(gè)encoing屬性是作何用的,我就不大清楚了,反正為了它,源碼寫了不少代碼。 類別中有三個(gè)方法,一個(gè)是setEncoding,一個(gè)是getEncoding,一個(gè)主要功能的filter。有兩個(gè)方法都是為了encoding來寫的。 在建構(gòu)函式中使用setEncoding方法直接用$this->setEncoding()就可。就可以把私有屬性設(shè)定好值了。 然後根據(jù)私有屬性的內(nèi)容來選擇使用什麼方法來使得字母變小寫。 我去,這個(gè)類別考慮的東西還真夠多的。其實(shí)核心程式碼就那兩句,strtolower((string) $value)。 這個(gè)類別很酷,我從來沒用過私有屬性??紤]問題也沒有作者那麼全面,各種驗(yàn)證,各種情況考量。例如,從構(gòu)造函數(shù)中就可以看出他考慮問題的全面性。
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']); }? ?總的來說還是值得佩服的。 下面談?wù)勥^濾器鏈,它的作用是將多個(gè)過濾器串聯(lián)起來配合使用。過濾器鏈就是多個(gè)過濾器的一個(gè)連接。在對指定的內(nèi)容進(jìn)行過濾時(shí),每個(gè)過濾器將按照其順序分別進(jìn)行過濾或轉(zhuǎn)換操作。當(dāng)所有的過濾操作都執(zhí)行完畢時(shí),過濾器鏈會(huì)傳回最終的過濾結(jié)果。 聽起來蠻有趣的??! 具體實(shí)現(xiàn)步驟是什麼? 首先要為類別Zend_Filter實(shí)例化一個(gè)對象,然後透過該實(shí)例的addFilter()方法向過濾器鏈中添加過濾器。 下面透過範(fàn)例示範(fàn)如何使用過濾器鏈對資料進(jìn)行多重過濾及轉(zhuǎn)換。 代碼:
<?php require_once 'Zend/Filter.php'; //加載Zend_Filter類 require_once 'Zend/Filter/Alpha.php'; //加載Zend_Filter_Alpha子類 require_once 'Zend/Filter/StringToUpper.php'; //加載Zend_Filter_StringToUpper子類 $filterChain = new Zend_Filter(); //創(chuàng)建過濾器鏈 $filterChain ->addFilter(new Zend_Filter_Alpha(" ")) ->addFilter(new Zend_Filter_StringToUpper());//向過濾器鏈中添加過濾器 $temp1 = "12345asdf67asdfasdf"; $temp2 = "#$%^!@fffff"; $temp3 = "Welcome to Bei Jing"; echo "內(nèi)容:".$temp1."<p>經(jīng)過過濾后為:"; echo $filterChain->filter($temp1); echo "<p>"; echo "內(nèi)容:".$temp2."<p>經(jīng)過過濾后為:"; echo $filterChain->filter($temp2); echo "<p>"; echo "內(nèi)容:".$temp3."<p>經(jīng)過過濾后為:"; echo $filterChain->filter($temp3); echo "<p>";? ?結(jié)果:內(nèi)容:12345asdf67asdfasdf
經(jīng)過濾後為:ASDFASDFASDF2345asdf67asdfasdf
分Jing過濾後為:WELCOME TO BEI JING
分析:
這裡的Alpha很強(qiáng)大啊,過濾數(shù)字和特殊字符,連空格都能過濾。還好我初始化的時(shí)候加了個(gè)參數(shù)" ",才使得空格保留了下來。
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); }? ?分析:這裡將內(nèi)容過濾,如果不是字母或空格,就統(tǒng)統(tǒng)去掉。用到的php方法是preg_replace。此外,還用到了正規(guī)表示式。 [^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è)方法來設(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í)透過屬性allowWhiteSpace來控制是否過濾空格。 剛才介紹了兩種過濾器,一個(gè)是StringToUpper,一個(gè)是Alpha,下面再介紹其它的一些過濾器。 首先是Alnum,過濾非數(shù)字和非字母的內(nèi)容,執(zhí)行filter()方法,將傳回純數(shù)字與字母的內(nèi)容,它是Zend_Filter_Alpha(過濾非字母)與Zend_Filter_Digits(過濾非數(shù)值)的並集。 具體的例子就不舉了,都差不多。 我們來看看它內(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); }? ?透過正則過濾除字母和數(shù)字之外的內(nèi)容。 下面出場的是HtmlEntities HTML過濾器。 代碼:
<?php require_once 'Zend/Filter/Htmlentities.php'; $filter = new Zend_Filter_HtmlEntities(); $temp1 = "<img src = './1.png' width='100px'>"; $temp2 = "<button>aaa</button>"; $temp3 = "<h1>Welcome to Bei Jing</h1>"; echo "內(nèi)容:".$temp1."<p>經(jīng)過過濾為:"; echo $filter->filter($temp1); echo "<p>"; echo "內(nèi)容:".$temp2."<p>經(jīng)過過濾為:"; echo $filter->filter($temp2); echo "<p>"; echo "內(nèi)容:".$temp3."<p>經(jīng)過過濾為:"; echo $filter->filter($temp3); echo "<p>";? ? 結(jié)果:
透過結(jié)果,我們看出它將html內(nèi)容還原成原始程式碼了。由於該過濾器是對函數(shù)htmlentities進(jìn)行的封裝,所以遵循該函數(shù)的規(guī)則。即將“”分別轉(zhuǎn)換為“”,經(jīng)過這樣的轉(zhuǎn)換,
對應(yīng)的HTML內(nèi)容變成了以其原始格式顯示的字串。
希望本文所述對大家基於Zend Framework框架的PHP程式設(shè)計(jì)有所幫助。
更多Zend Framework過濾器Zend_Filter用法詳解相關(guān)文章請關(guān)注PHP中文網(wǎng)!

熱AI工具

Undress AI Tool
免費(fèi)脫衣圖片

Undresser.AI Undress
人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強(qiáng)大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6
視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)