Php filters sensitive word instance code .? Php *** CreatedbyJetBrainsPhpStorm. * User: lsl * Date: 13-8-28 * Time: * sensitive word filtering tool class * usage method * echoFilterTools: filterContent (quot; fuck me! /**
* Created by JetBrains PhpStorm.
* User: lsl
* Date: 13-8-28
* Time: PM
* Sensitive word filtering tool
* Usage
* Echo FilterTools: filterContent ("Fuck, I fuck one-color Wolf, two-mountain food", "*", DIR. "config/word.txt", $ GLOBALS ["p_memcache"] ["bad_words"]);
*/
Class FilterTools {
Public static $ keyword = array ();
/**
* Load sensitive words from files
* @ Param $ filename
* @ Return array
*/
Static function getBadWords ($ filename ){
$ File_handle = fopen ($ filename, "r ");
While (! Feof ($ file_handle )){
$ Line = trim (fgets ($ file_handle ));
Array_push (self: $ keyword, $ line );
}
Fclose ($ file_handle );
Return self: $ keyword;
}
/**
* @ Param $ content: string to be processed
* @ Param $ the character after target is replaced
* @ Param $ filename sensitive word configuration file
* @ Param $ memconfig cache configuration file
* @ Return: the processed string
*/
Static function filterContent ($ content, $ target, $ filename, $ memconfig ){
$ Mem = new BadWordsMemcache ($ filename, $ memconfig );
$ Keyword = $ mem-> getList ();
If (count ($ keyword) = 0 ){
$ Keyword = self: getBadWords ($ filename );
}
Return strtr ($ content, array_combine ($ keyword, array_fill (0, count ($ keyword), $ target )));
}
}
/**
* Sensitive word cache processing
* Class BadWordsMemcache
* User: lsl
*/
Class BadWordsMemcache {
Var $ memcache;
Var $ key;
Var $ list;
Var $ filename;
Function _ construct ($ filename, $ memconfig ){
$ This-> filename = $ filename;
If (! Class_exists ("P_Memcache ")){
Require_once DIR. "lib/memcache. class. php ";
}
$ This-> key = "bad_words ";
$ This-> memcache = new P_Memcache ();
$ This-> memcache-> config = $ memconfig;
$ This-> memcache-> connect ();
Print_r ($ this-> memcache );
$ This-> init ();
}
Function _ destruct (){
$ This-> memcache-> close ();
}
/**
* Initialization
* @ Param bool $ isReset
*/
Function init ($ isReset = false ){
$ This-> list = $ this-> memcache-> get ($ this-> key )? $ This-> memcache-> get ($ this-> key): array ();
If (count ($ this-> list) = 0 | $ isReset ){
$ This-> list = filterTools: getBadWords ($ this-> filename );
$ This-> memcache-> set ($ this-> key, $ this-> list );
$ Log_data = Log: formatData ($ this-> list );
Log: logWrite ($ log_data, 'Bad. word', 'init ');
}
}
/**
* Get the list
* @ Return mixed
*/
Function getList (){
Return $ this-> list;
}
}
Http://www.bkjia.com/PHPjc/371371.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/371371.htmlTechArticle? Php/*** CreatedbyJetBrainsPhpStorm. * User: lsl * Date: 13-8-28 * Time: * sensitive word filtering tool class * usage method * echoFilterTools: filterContent (quot; fuck me...