《PHP編程:Zend Framework過濾器Zend_Filter用法詳解》要點(diǎn):
本文介紹了PHP編程:Zend Framework過濾器Zend_Filter用法詳解,希望對您有用。如果有疑問,可以聯(lián)系我們。
PHP實(shí)例本文實(shí)例講述了Zend Framework過濾器Zend_Filter用法.分享給大家供大家參考,具體如下:
PHP實(shí)例引言:過濾器是對輸入內(nèi)容進(jìn)行過濾,清除其中不符合過濾規(guī)則的內(nèi)容,并將其余內(nèi)容返回的過程.
PHP實(shí)例Zend中有個(gè)Zend_Filter組件用來實(shí)現(xiàn)過濾的功能.其中有個(gè)Zend_Filter_Interface子類,該子類為實(shí)現(xiàn)一般過濾器提供了接口.
PHP實(shí)例要實(shí)現(xiàn)過濾器類,需要實(shí)現(xiàn)該接口中一個(gè)名為filter()的方法.
PHP實(shí)例下面通過實(shí)例來演示如何使用Zend_Filter中定義的過濾器,該例演示如何實(shí)現(xiàn)字母轉(zhuǎn)小寫的功能.
PHP實(shí)例代碼:
PHP實(shí)例
<?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);
PHP實(shí)例結(jié)果:
PHP實(shí)例內(nèi)容:ABCDefGH
經(jīng)過濾后為:abcdefgh
內(nèi)容:我愛Nan Jing
經(jīng)過濾后為:我愛nan jing
PHP實(shí)例為什么如此神奇呢?不禁讓我想探索一下其內(nèi)部的構(gòu)造!下面來研讀一下其內(nèi)部的工作原理.
PHP實(shí)例
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);
}
}
PHP實(shí)例研讀:
PHP實(shí)例源代碼意思大概是先實(shí)現(xiàn)Zend_Filter_Interface接口.
PHP實(shí)例定義一個(gè)私有變量$_encoding,初始值為null,一般私有變量都是以_下劃線開頭.
PHP實(shí)例然后通過構(gòu)造函數(shù)進(jìn)行初始化工作,設(shè)置encoding.
PHP實(shí)例至于這個(gè)encoing屬性是作何用的,我就不大清楚了,反正為了它,源碼寫了不少代碼.
PHP實(shí)例類中有三個(gè)方法,一個(gè)是setEncoding,一個(gè)是getEncoding,一個(gè)主要功能的filter.有兩個(gè)方法都是為了encoding來寫的.
PHP實(shí)例在構(gòu)造函數(shù)中使用setEncoding方法直接用$this->setEncoding()就可.就可以把私有屬性設(shè)置好值了.
PHP實(shí)例然后根據(jù)私有屬性的內(nèi)容來選擇使用什么方法來使得字母變小寫.
PHP實(shí)例我去,這個(gè)類考慮的東西還真夠多的.其實(shí)核心代碼就那兩句,strtolower((string) $value).
PHP實(shí)例這個(gè)類很酷,我從來沒用過私有屬性.考慮問題也沒有作者那么全面,各種驗(yàn)證,各種情況考慮.比如,
PHP實(shí)例從構(gòu)造函數(shù)中就可以看出他考慮問題的全面性.
PHP實(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']);
}
PHP實(shí)例總的來說還是值得佩服的.
PHP實(shí)例下面談?wù)勥^濾器鏈,它的作用是將多個(gè)過濾器串聯(lián)起來配合使用.過濾器鏈就是多個(gè)過濾器的一個(gè)連接.在對指定的內(nèi)容進(jìn)行過濾時(shí),
PHP實(shí)例每個(gè)過濾器將按照其順序分別進(jìn)行過濾或者轉(zhuǎn)化操作.當(dāng)所有的過濾操作都執(zhí)行完畢時(shí),過濾器鏈返回最終的過濾結(jié)果.
PHP實(shí)例聽起來蠻有趣的啊!
PHP實(shí)例具體實(shí)現(xiàn)步驟是什么呢?
PHP實(shí)例首先要為類Zend_Filter實(shí)例化一個(gè)對象,然后通過該實(shí)例的addFilter()方法向過濾器鏈中添加過濾器.
PHP實(shí)例下面通過示例演示如何使用過濾器鏈對數(shù)據(jù)進(jìn)行多重過濾及轉(zhuǎn)化.
PHP實(shí)例代碼:
PHP實(shí)例
<?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>";
PHP實(shí)例結(jié)果:
PHP實(shí)例內(nèi)容:12345asdf67asdfasdf
經(jīng)過過濾后為:ASDFASDFASDF
內(nèi)容:#$%^!@fffff
經(jīng)過過濾后為:FFFFF
內(nèi)容:Welcome to Bei Jing
經(jīng)過過濾后為:WELCOME TO BEI JING
PHP實(shí)例分析:
PHP實(shí)例這里的Alpha很強(qiáng)大啊,過濾數(shù)字和特殊字符,連空格都能過濾.還好我初始化的時(shí)候加了個(gè)參數(shù)" ",才使得空格保留了下來.
PHP實(shí)例為何如此神奇呢?
PHP實(shí)例核心代碼就這一塊
PHP實(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);
}
PHP實(shí)例分析:這里對內(nèi)容進(jìn)行過濾,如果不是字母或者空格,就統(tǒng)統(tǒng)去掉.用到的php方法是preg_replace.此外,還用到了正則表達(dá)式.[^a-zA-Z]表示除此之外的其他字符.
PHP實(shí)例這里的$whiteSpace成員屬性,是初始化的時(shí)候設(shè)置的,具體代碼如下:
PHP實(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')
);
}
}
PHP實(shí)例此外,還有兩個(gè)方法來設(shè)置是否允許有空格和獲取是否設(shè)置了允許空格.
PHP實(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;
}
PHP實(shí)例剖析完之后,我們似乎就更了解它的構(gòu)造了,就是使用正則過濾而已.同時(shí)通過屬性allowWhiteSpace來控制是否過濾空格.
PHP實(shí)例剛才介紹了兩種過濾器,一個(gè)是StringToUpper,一個(gè)是Alpha,下面再介紹其它的一些過濾器.
PHP實(shí)例首先是Alnum,過濾非數(shù)字和非字母的內(nèi)容,執(zhí)行filter()方法,將返回純數(shù)字與字母的內(nèi)容,它是Zend_Filter_Alpha(過濾非字母)與Zend_Filter_Digits(過濾非數(shù)值)的并集.
PHP實(shí)例具體的例子就不舉了,都差不多.
PHP實(shí)例我們來看看它內(nèi)部的構(gòu)造,
PHP實(shí)例
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);
}
PHP實(shí)例通過正則過濾除字母和數(shù)字之外的內(nèi)容.
PHP實(shí)例下面出場的是HtmlEntities HTML過濾器.
PHP實(shí)例代碼:
PHP實(shí)例
<?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>";
PHP實(shí)例結(jié)果:
PHP實(shí)例
PHP實(shí)例通過結(jié)果,我們看出它將html內(nèi)容還原成原始代碼了.由于該過濾器是對函數(shù)htmlentities進(jìn)行的封裝,所以遵循該函數(shù)的規(guī)則.即將“<”與“>”分別轉(zhuǎn)換為“<”與“>”,經(jīng)過這樣的轉(zhuǎn)換,
PHP實(shí)例相應(yīng)的HTML內(nèi)容就變成了以其原始格式顯示的字符串.
PHP實(shí)例更多關(guān)于zend相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Zend FrameWork框架入門教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《Yii框架入門及常用技巧總結(jié)》、《ThinkPHP入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
PHP實(shí)例希望本文所述對大家基于Zend Framework框架的PHP程序設(shè)計(jì)有所幫助.
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/2452.html