本文实例讲述了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>";
结果:
通过结果,我们看出它将html内容还原成原始代码了。由于该过滤器是对函数htmlentities进行的封装,所以遵循该函数的规则。即将“8bc3bdc820fe3bcce59e00651a21773d”分别转换为“8bc3bdc820fe3bcce59e00651a21773d”,经过这样的转换,
相应的html内容就变成了以其原始格式显示的字符串。
希望本文所述对大家基于zend framework框架的php程序设计有所帮助。
更多zend framework过滤器zend_filter用法详解。