| <? Php /** String filter class * Date: 2013-01-09 * Author: fdipzone * Ver: v1.0 * * Func: * Replace public replace with invalid characters * Public check whether illegal characters are contained * Private protect_white_list protection White List * Private resume_white_list restore the White List * Convert private getval white list 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 = $ white_list; $ This-> _ black_list = $ black_list; $ This-> _ replacement = $ replacement; } /** Replace invalid characters * @ Param String $ content refers to the String to be replaced. * @ Return String the String after replacement */ 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 whether there are invalid tokens * @ 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; } /** Protect the 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 the 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; } /** Restore the white list key to value * @ Param Array $ matches the key of white_list * @ 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 ?> |