StrFilter.class.php
<?php/** String Filter class * date:2013-01-09 * author:fdipzone * ver:v1.0 * Func: * Public Replace replace illegal character * Public check check for illegal characters * Private protect_white_list protection White list * Private resume_ White_list Restore White list * Private getval Whitelist key to value */class strfilter{//class start Private $_
White_list = Array ();
Private $_black_list = Array ();
Private $_replacement = ' * ';
Private $_ltag = ' [[#];
Private $_rtag = ' # #]] '; /** * @param array $white _list * @param array $black _list * @param String $replacement/Public function __construct ($white _list=array (), $black _list=array (), $replacement = ' * ') {$this->_white_list = $whit
E_list;
$this->_black_list = $black _list;
$this->_replacement = $replacement; /** Replace the illegal character * @param string $content the strings to be replaced * @return stringReplaced string */Public Function replace ($content) {if (!isset ($content) | | | $content = = ") {
Return ";
}//Protect white list $content = $this->protect_white_list ($content);
Replace black list if ($this->_black_list) {foreach ($this->_black_list as $val) {
$content = Str_replace ($val, $this->_replacement, $content);
}///Resume white list $content = $this->resume_white_list ($content);
return $content; /** Check for illegal character * @param string $content Strings * @return Boolean/Public function
Check ($content) {if (!isset ($content) | | | $content = = ") {return true;
}//Protect white list $content = $this->protect_white_list ($content); Check if ($tHis->_black_list) {foreach ($this->_black_list as $val) {if (Strstr ($content, $val)!= '
') {return false;
}} return true; /** Protection Whitelist * @param string $content strings * @return string/Private Function Prot Ect_white_list ($content) {if ($this->_white_list) {foreach ($this->_white_list as $key => $va
L) {$content = Str_replace ($val, $this->_ltag. $key. $this->_rtag, $content);
} return $content; /** Restore White list * @param string $content * @return string/Private Function Resume_w Hite_list ($content) {if ($this->_white_list) {$content = Preg_replace_callback ("/\[\[## (. *?)
##\]\].*?/si ", Array ($this, ' getval '), $content);
return $content;
}
/** White list key reverts to value * @param Array $matches match white_list key * @return String white_list val */ Private Function Getval ($matches) {return isset ($this->_white_list[$matches [1]])? $this->_white_list[$mat CHES[1]]: "; Key->val}}//class end?>
Demo
<?php
Header ("Content-type:text/html;charset=utf8");
Require ("StrFilter.class.php");
$white = array (' Cock silk ', ' Cao ');
$black = array (' Cock ', ' fuck ');
$content = "I fuck, caocao you are cock silk, I cock you ah";
$obj = new Strfilter ($white, $black);
echo $obj->replace ($content);
? >
SOURCE Download Address: http://download.csdn.net/detail/fdipzone/5789781
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/