Zend Framework过滤器Zend_Filter用法详解
本篇章节讲解Zend Framework过滤器Zend_Filter用法。分享给大家供大家参考,具体如下: 引言:Zend中有个Zend_Filter组件用来实现过滤的功能。其中有个Zend_Filter_Interface子类,该子类为实现一般过滤器提供了接口。 要实现过滤器类,需要实现该接口中一个名为filter()的方法。 下面通过实例来演示如何使用Zend_Filter中定义的过滤器,该例演示如何实现字母转小写的功能。 代码: 经过滤后为:";
echo $filter->filter($temp1);
echo "
"; echo "内容:".$temp2." 经过滤后为:"; echo $filter->filter($temp2); 结果:
为什么如此神奇呢?不禁让我想探索一下其内部的构造!下面来研读一下其内部的工作原理。 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);
}
}
研读:
setEncoding()就可。就可以把私有属性设置好值了。
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()方法向过滤器链中添加过滤器。 下面通过示例演示如何使用过滤器链对数据进行多重过滤及转化。 代码: addFilter(new Zend_Filter_Alpha(" "))
->addFilter(new Zend_Filter_StringToUpper());//向过滤器链中添加过滤器
$temp1 = "12345asdf67asdfasdf";
$temp2 = "#$%^!@fffff";
$temp3 = "Welcome to Bei Jing";
echo "内容:".$temp1."
经过过滤后为:"; echo $filterChain->filter($temp1); echo " "; echo "内容:".$temp2." 经过过滤后为:"; echo $filterChain->filter($temp2); echo " "; echo "内容:".$temp3." 经过过滤后为:"; echo $filterChain->filter($temp3); echo " "; 结果:
分析:这里的Alpha很强大啊,过滤数字和特殊字符,连空格都能过滤。还好我初始化的时候加了个参数" ",才使得空格保留了下来。 为何如此神奇呢? 核心代码就这一块 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成员属性,是初始化的时候设置的,具体代码如下: 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')
);
}
}
此外,还有两个方法来设置是否允许有空格和获取是否设置了允许空格。 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(过滤非数值)的并集。 具体的例子就不举了,都差不多。 我们来看看它内部的构造, 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过滤器。 代码: " ;="" $temp2="" $temp3="
Welcome to Bei Jing" echo="" "内容:".$temp1."经过过滤为:"; echo $filter->filter($temp1); echo " "; echo "内容:".$temp2." 经过过滤为:"; echo $filter->filter($temp2); echo " "; echo "内容:".$temp3." 经过过滤为:"; echo $filter->filter($temp3); echo " "; 结果: 通过结果,我们看出它将html内容还原成原始代码了。由于该过滤器是对函数htmlentities进行的封装,所以遵循该函数的规则。即将“<”与“>”分别转换为“<”与“>”,经过这样的转换, 相应的HTML内容就变成了以其原始格式显示的字符串。 更多关于zend相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》及《》 希望本文所述对大家基于Zend Framework框架的PHP程序设计有所帮助。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |