欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Zend Framework過濾器Zend_Filter用法詳解

 更新時(shí)間:2016年12月09日 11:24:45   作者:牛逼的霍嘯林  
這篇文章主要介紹了Zend Framework過濾器Zend_Filter用法,結(jié)合實(shí)例形式分析了過濾器Zend_Filter的功能、原理、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下

本文實(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í)現(xiàn)一般過濾器提供了接口。

要實(shí)現(xiàn)過濾器類,需要實(shí)現(xiàn)該接口中一個(gè)名為filter()的方法。

下面通過實(shí)例來演示如何使用Zend_Filter中定義的過濾器,該例演示如何實(shí)現(xiàn)字母轉(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
經(jīng)過濾后為:abcdefgh
內(nèi)容:我愛Nan Jing
經(jīng)過濾后為:我愛nan jing

為什么如此神奇呢?不禁讓我想探索一下其內(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í)現(xiàn)Zend_Filter_Interface接口。

定義一個(gè)私有變量$_encoding,初始值為null,一般私有變量都是以_下劃線開頭。

然后通過構(gòu)造函數(shù)進(jìn)行初始化工作,設(shè)置encoding。

至于這個(gè)encoing屬性是作何用的,我就不大清楚了,反正為了它,源碼寫了不少代碼。

類中有三個(gè)方法,一個(gè)是setEncoding,一個(gè)是getEncoding,一個(gè)主要功能的filter。有兩個(gè)方法都是為了encoding來寫的。

在構(gòu)造函數(shù)中使用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í),過濾器鏈返回最終的過濾結(jié)果。

聽起來蠻有趣的??!

具體實(shí)現(xiàn)步驟是什么呢?

首先要為類Zend_Filter實(shí)例化一個(gè)對象,然后通過該實(shí)例的addFilter()方法向過濾器鏈中添加過濾器。

下面通過示例演示如何使用過濾器鏈對數(shù)據(jù)進(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)過過濾后為:ASDFASDFASDF
內(nèi)容:#$%^!@fffff
經(jīng)過過濾后為:FFFFF
內(nèi)容:Welcome to Bei Jing
經(jīng)過過濾后為: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)容進(jìn)行過濾,如果不是字母或者空格,就統(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è)方法來設(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)造了,就是使用正則過濾而已。同時(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)換為“&lt;”與“&gt;”,經(jīng)過這樣的轉(zhuǎn)換,

相應(yīng)的HTML內(nèi)容就變成了以其原始格式顯示的字符串。

更多關(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ù)庫操作技巧匯總

希望本文所述對大家基于Zend Framework框架的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • php微信公眾號開發(fā)之現(xiàn)金紅包

    php微信公眾號開發(fā)之現(xiàn)金紅包

    這篇文章主要為大家詳細(xì)介紹了php微信公眾號開發(fā)之現(xiàn)金紅包,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • PHP之密碼加密的幾種方式

    PHP之密碼加密的幾種方式

    這篇文章主要是針對PHP實(shí)現(xiàn)密碼加密的幾種方式的詳細(xì)介紹,有需要的朋友可以參考下本文。
    2015-07-07
  • php生成縮略圖示例代碼分享(使用gd庫實(shí)現(xiàn))

    php生成縮略圖示例代碼分享(使用gd庫實(shí)現(xiàn))

    分享一個(gè)利用php的GD庫生成縮略圖的例子,大家參考使用吧
    2014-01-01
  • PHP 中常量的知識整理

    PHP 中常量的知識整理

    這篇文章主要介紹了PHP 中常量的知識整理的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • 淺析php工廠模式

    淺析php工廠模式

    本文通過實(shí)例向大家展示了php設(shè)計(jì)模式中的工廠模式,工廠模式分為三種:簡單工廠、工廠方法、抽象工廠。想學(xué)習(xí)的小伙伴們千萬不要錯(cuò)過。
    2014-11-11
  • ThinkPHP框架分布式數(shù)據(jù)庫連接方法詳解

    ThinkPHP框架分布式數(shù)據(jù)庫連接方法詳解

    這篇文章主要介紹了ThinkPHP框架分布式數(shù)據(jù)庫連接方法,結(jié)合實(shí)例形式詳細(xì)分析了thinkPHP框架針對分布式數(shù)據(jù)庫的連接方法、操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-03-03
  • PHP面向?qū)ο蠼坛讨远x類

    PHP面向?qū)ο蠼坛讨远x類

    所謂面向?qū)ο缶褪鞘裁磿r(shí)候什么東西做什么,我們設(shè)計(jì)類的時(shí)候需要想的就是怎么做的內(nèi)容,那么怎么樣的一個(gè)類才算是符合OOP的思想呢,答案是:這個(gè)類寫好之后,在使用的過程中,能準(zhǔn)確的代表一個(gè)事物,在書寫的時(shí)候代碼要和思維描述一致,即這個(gè)東西做什么。
    2014-06-06
  • laravel請求參數(shù)校驗(yàn)方法

    laravel請求參數(shù)校驗(yàn)方法

    今天小編就為大家分享一篇laravel請求參數(shù)校驗(yàn)方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10
  • CodeIgniter配置之SESSION用法實(shí)例分析

    CodeIgniter配置之SESSION用法實(shí)例分析

    這篇文章主要介紹了CodeIgniter配置之SESSION用法,結(jié)合實(shí)例形式分析了PHP中session的運(yùn)行原理并演示了CI框架下session的配置與使用方法,需要的朋友可以參考下
    2016-01-01
  • php實(shí)現(xiàn)爬取和分析知乎用戶數(shù)據(jù)

    php實(shí)現(xiàn)爬取和分析知乎用戶數(shù)據(jù)

    本文給大家介紹的是利用php的curl編寫的爬取知乎用戶數(shù)據(jù)的爬蟲,并分析用戶的各種屬性,有需要的小伙伴可以參考下
    2016-01-01

最新評論