/** String Filter Class * date:2013-01-09 * Author:fdipzone * ver:v1.0 * * Func: * Public replace replaces illegal characters * Public check Check to see if illegal characters are included * Private protect_white_list protection Whitelist * Private resume_white_list Restore white list * Private getval Whitelist key converted 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 = $white _list; $this->_black_list = $black _list; $this->_replacement = $replacement; } /** replacing illegal characters * @param string $content to swap * string replaced by @return strings */ 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 self-characters * @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; } White List of/** protection * @param string $content strings * @return String */ Private Function Protect_white_list ($content) { if ($this->_white_list) { foreach ($this->_white_list as $key = + $val) { $content = Str_replace ($val, $this->_ltag. $key. $this->_rtag, $content); } } return $content; } /** to restore white list * @param String $content * @return String */ Private Function Resume_white_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[$matches [1]]: '; Key->val } }//Class end ?> |