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