The StrFilter.class.php class files are as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114-115 |
<?php /** String Filter class * date: 2013-01-09 * author: fdipzone * VER:&N bsp; v1.0 * * func: * Public replace replace illegal characters * Public CHECK&NB sp; check for illegal characters * Private protect_white_list protection Whitelist * Private Resume_white_ List Restore White list * Private getval Whitelist key to value */ class strfilter{//C Lass start private $_white_list = Array (); private = Array (); private $_replacement = ' * '; private $_ltag = ' [[# # '; Private $_rtag = ' # #]] '; /** * @param Array $white _list & nbsp; * @param Array $black _list * @param String $replacement */ & Nbsp;public function __construct ($white _list=aRray (), $black _list=array (), $replacement = ' * ') { $this->_white_list = $white _list; $this->_black_list = $black _list; $this->_ Replacement = $replacement; } /** replace illegal characters * @ param string $content the strings to be replaced * @return the string string */& nbsp 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) { &Nbsp;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 to see if there is an illegal character * @param string $content string * @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 white list * @param string $content string * @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; } /** restore white list * @param String $content * @return string & nbsp;*/ private function Resume_white_list ($content) { if ($this- >_white_list) { $content = Preg_replace_callback ("/[[##" (. *?) ##]].*?/si ", Array ($this, ' getval '), $content); } return $content; } /** Whitelist key revert 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 ?> |
The demo example is as follows:
1 2 3 4 5 6 7 8 9 10 11 12-13 |
<?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); ?> |