Zend Framework过滤器Zend_Filter用法详解


Posted in PHP onDecember 09, 2016

本文实例讲述了Zend Framework过滤器Zend_Filter用法。分享给大家供大家参考,具体如下:

引言:过滤器是对输入内容进行过滤,清除其中不符合过滤规则的内容,并将其余内容返回的过程

Zend中有个Zend_Filter组件用来实现过滤的功能。其中有个Zend_Filter_Interface子类,该子类为实现一般过滤器提供了接口。

要实现过滤器类,需要实现该接口中一个名为filter()的方法。

下面通过实例来演示如何使用Zend_Filter中定义的过滤器,该例演示如何实现字母转小写的功能。

代码:

<?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);

结果:

内容:ABCDefGH
经过滤后为:abcdefgh
内容:我爱Nan Jing
经过滤后为:我爱nan jing

为什么如此神奇呢?不禁让我想探索一下其内部的构造!下面来研读一下其内部的工作原理。

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);
  }
}

研读:

源代码意思大概是先实现Zend_Filter_Interface接口。

定义一个私有变量$_encoding,初始值为null,一般私有变量都是以_下划线开头。

然后通过构造函数进行初始化工作,设置encoding。

至于这个encoing属性是作何用的,我就不大清楚了,反正为了它,源码写了不少代码。

类中有三个方法,一个是setEncoding,一个是getEncoding,一个主要功能的filter。有两个方法都是为了encoding来写的。

在构造函数中使用setEncoding方法直接用$this->setEncoding()就可。就可以把私有属性设置好值了。

然后根据私有属性的内容来选择使用什么方法来使得字母变小写。

我去,这个类考虑的东西还真够多的。其实核心代码就那两句,strtolower((string) $value)。

这个类很酷,我从来没用过私有属性。考虑问题也没有作者那么全面,各种验证,各种情况考虑。比如,

从构造函数中就可以看出他考虑问题的全面性。

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']);
}

总的来说还是值得佩服的。

下面谈谈过滤器链,它的作用是将多个过滤器串联起来配合使用。过滤器链就是多个过滤器的一个连接。在对指定的内容进行过滤时,

每个过滤器将按照其顺序分别进行过滤或者转化操作。当所有的过滤操作都执行完毕时,过滤器链返回最终的过滤结果。

听起来蛮有趣的啊!

具体实现步骤是什么呢?

首先要为类Zend_Filter实例化一个对象,然后通过该实例的addFilter()方法向过滤器链中添加过滤器。

下面通过示例演示如何使用过滤器链对数据进行多重过滤及转化。

代码:

<?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>";

结果:

内容:12345asdf67asdfasdf
经过过滤后为:ASDFASDFASDF
内容:#$%^!@fffff
经过过滤后为:FFFFF
内容:Welcome to Bei Jing
经过过滤后为:WELCOME TO BEI JING

分析:

这里的Alpha很强大啊,过滤数字和特殊字符,连空格都能过滤。还好我初始化的时候加了个参数" ",才使得空格保留了下来。

为何如此神奇呢?

核心代码就这一块

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方法是preg_replace。此外,还用到了正则表达式。[^a-zA-Z]表示除此之外的其他字符。

这里的$whiteSpace成员属性,是初始化的时候设置的,具体代码如下:

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')
      );
    }
}

此外,还有两个方法来设置是否允许有空格和获取是否设置了允许空格。

/**
* 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;
}

剖析完之后,我们似乎就更了解它的构造了,就是使用正则过滤而已。同时通过属性allowWhiteSpace来控制是否过滤空格。

刚才介绍了两种过滤器,一个是StringToUpper,一个是Alpha,下面再介绍其它的一些过滤器。

首先是Alnum,过滤非数字和非字母的内容,执行filter()方法,将返回纯数字与字母的内容,它是Zend_Filter_Alpha(过滤非字母)与Zend_Filter_Digits(过滤非数值)的并集。

具体的例子就不举了,都差不多。

我们来看看它内部的构造,

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);
}

通过正则过滤除字母和数字之外的内容。

下面出场的是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 "内容:".$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>";

结果:

Zend Framework过滤器Zend_Filter用法详解

通过结果,我们看出它将html内容还原成原始代码了。由于该过滤器是对函数htmlentities进行的封装,所以遵循该函数的规则。即将“<”与“>”分别转换为“<”与“>”,经过这样的转换,

相应的HTML内容就变成了以其原始格式显示的字符串。

希望本文所述对大家基于Zend Framework框架的PHP程序设计有所帮助。

PHP 相关文章推荐
vBulletin HACK----关于排版的两个HACK
Oct 09 PHP
解析CI即CodeIgniter框架在Nginx下的重写规则
Jun 03 PHP
浅析php面向对象public private protected 访问修饰符
Jun 30 PHP
浅析php与数据库代码开发规范
Aug 08 PHP
Destoon模板制作简明教程
Jun 20 PHP
php计算两个整数的最大公约数常用算法小结
Mar 05 PHP
php通过前序遍历树实现无需递归的无限极分类
Jul 10 PHP
WordPress中给文章添加自定义字段及后台编辑功能区域
Dec 19 PHP
PHP表单数据写入MySQL数据库的代码
May 31 PHP
PHP处理CSV表格文件的常用操作方法总结
Jul 01 PHP
bindParam和bindValue的区别以及在Yii2中的使用详解
Mar 12 PHP
PHP基于openssl实现非对称加密代码实例
Jun 19 PHP
Zend Framework实现自定义过滤器的方法
Dec 09 #PHP
Zend Framework校验器Zend_Validate用法详解
Dec 09 #PHP
浅谈PHP中try{}catch{}的使用方法
Dec 09 #PHP
浅谈htmlentities 、htmlspecialchars、addslashes的使用方法
Dec 09 #PHP
浅谈PHP的数据库接口和技术
Dec 09 #PHP
magento后台无法登录解决办法的两种方法
Dec 09 #PHP
Zend Framework常用校验器详解
Dec 09 #PHP
You might like
收听短波不可能有声音清晰的品质吗
2021/03/01 无线电
php smarty 二级分类代码和模版循环例子
2011/06/16 PHP
php数组的一些常见操作汇总
2011/07/17 PHP
PHP类与对象中的private访问控制的疑问
2012/11/01 PHP
基于PHP实现等比压缩图片大小
2016/03/04 PHP
分析PHP中单双引号的误区和双引号小隐患
2016/07/19 PHP
javascript编程起步(第一课)
2007/01/10 Javascript
JS加ASP二级域名转向的代码
2007/05/17 Javascript
Mootools 1.2教程 同时进行多个形变动画
2009/09/15 Javascript
jQuery 位置函数offset,innerWidth,innerHeight,outerWidth,outerHeight,scrollTop,scrollLeft
2010/03/23 Javascript
两个Javascript小tip资料
2010/11/23 Javascript
分享2个jQuery插件--jquery.fileupload与artdialog
2014/12/26 Javascript
jQuery事件绑定on()、bind()与delegate() 方法详解
2015/06/03 Javascript
nodejs实现获取当前url地址及url各种参数值
2015/06/25 NodeJs
js点击按钮实现带遮罩层的弹出视频效果
2015/12/19 Javascript
javascript关于继承解析
2016/05/10 Javascript
JavaScript和JQuery获取DIV值的方法示例
2017/03/07 Javascript
vue中,在本地缓存中读写数据的方法
2018/09/21 Javascript
微信小程序搜索功能(附:小程序前端+PHP后端)
2019/02/28 Javascript
微信小程序左滑删除实现代码实例
2019/09/16 Javascript
Nuxt页面级缓存的实现
2020/03/09 Javascript
python实现判断数组是否包含指定元素的方法
2015/07/15 Python
浅谈python 四种数值类型(int,long,float,complex)
2016/06/08 Python
在Python中定义和使用抽象类的方法
2016/06/30 Python
python模块之sys模块和序列化模块(实例讲解)
2017/09/13 Python
Python算法之求n个节点不同二叉树个数
2017/10/27 Python
浅谈使用Python变量时要避免的3个错误
2017/10/30 Python
CSS3实现头像旋转效果
2017/03/13 HTML / CSS
英国户外玩具儿童游乐设备网站:TP Toys(蹦床、攀爬框架、秋千、滑梯和游戏屋)
2018/04/09 全球购物
上海某公司.net方向笔试题
2014/09/14 面试题
大二学期个人自我评价
2014/01/13 职场文书
励志演讲稿范文
2014/04/29 职场文书
校园安全标语
2014/06/07 职场文书
党支部意见范文
2015/06/02 职场文书
Mysql Show Profile
2021/04/05 MySQL
mysql中DCL常用的用户和权限控制
2022/03/31 MySQL