PHP filter Sensitive Word instance code

Source: Internet
Author: User
Tags config

<?php


  /** 


* Created by JetBrains phpstorm.


* USER:LSL


* date:13-8-28


* Time: Afternoon 2:58


* Sensitive word Filter tool class


* Use method


* echo filtertools::filtercontent (&quot; your mother i fuck color wolf hybrid two mountain food &quot;,&quot;*&quot;,dir.&quot ; config/word.txt&quot; $GLOBALS [&quot;p_memcache&quot;] [&quot;bad_words&quot;]);


   */ 


   


class Filtertools {


Public static $keyword = Array ();


      /** 


* Load sensitive words from a file


* @param $filename


* @return Array


       */ 


static function Getbadwords ($filename) {


$file _handle = fopen ($filename, &quot;r&quot;);


while (!feof ($file _handle)) {


$line = Trim (fgets ($file _handle));


Array_push (self:: $keyword, $line);


          } 


fclose ($file _handle);


return self:: $keyword;


      } 


   


      /** 


* @param $content to be processed string


* @param $target replaced character


* @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-&gt;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


* Class badwordsmemcache


* USER:LSL


   */ 


class badwordsmemcache{


var $memcache;


var $key;


var $list;


var $filename;


function __construct ($filename, $memconfig) {


$this-&gt;filename = $filename;


if (!class_exists &quot; p_memcache&quot;)) {


require_once dir.&quot;lib/memcache.class.php&quot;;


          } 


$this-&gt;key = &quot;bad_words&quot;;


$this-&gt;memcache = new P_memcache ();


$this-&gt;memcache-&gt;config = $memconfig;


$this-&gt;memcache-&gt;connect ();


Print_r ($this-&gt;memcache);


$this-&gt;init ();


      } 


function __destruct () {


$this-&gt;memcache-&gt;close ();


      } 


   


      /** 


* Initialization


* @param bool $isReset


       */ 


function init ($isReset = False) {


$this-&gt;list = $this-&gt;memcache-&gt;get ($this-&gt;key)? $this-&gt;memcache-&gt;ge T ($this-&gt;key): Array ();


if (count ($this-&gt;list) ==0 | | $isReset) {


$this-&gt;list = filtertools::getbadwords ($this-&gt;filename);


$this-&gt;memcache-&gt;set ($this-&gt;key, $this-&gt;list);


$log _data = log::formatdata ($this-&gt;list);


Log::logwrite ($log _data, ' bad.words ', ' init ');


          } 


      } 


   


      /** 


* Get list


* @return Mixed


       */ 


function GetList () {


return $this-&gt;list;


      } 


  } 

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.