Php sensitive word filtering example code _ PHP Tutorial

Source: Internet
Author: User
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.