This article describes how to replace sensitive strings in php, including common functions such as illegal string detection, whitelist, blacklist, and character replacement, for more information about how to replace sensitive strings in php, see the example in this article. it has a wide range of application values in php program development. Share it with you for your reference. The specific method is as follows:
The StrFilter. class. php class file is as follows:
<? Php/** string filter class * Date: 2013-01-09 * Author: fdipzone * Ver: v1.0 ** Func: * public replace replacement of invalid characters * public check whether illegal characters * private protect_white_list protection White List * private resume_white_list restoration White List * 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: The String to be replaced by the delimiter * @ return String: the String after replacement */public function replace ($ co Ntent) {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 is an invalid self character * @ param String $ content String * @ re Turn 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 ($ c Ontent) {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?>
Demo example:
<? Php header ("content-type: text/html; charset = utf8"); require ("StrFilter. class. php "); $ white = array ('Diaosi', 'caocao'); $ black = array ('diao', 'taocao'); $ content =" Fuck, cao, you are a diaosi, I am jealous of you, Ah "; $ obj = new StrFilter ($ white, $ black); echo $ obj-> replace ($ content);?>
Click here to download the complete instance code.
I hope this article will help you learn php programming.