日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php过滤敏感词实例代码

發布時間:2025/7/25 php 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php过滤敏感词实例代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

php過濾敏感詞實例代碼


<?php
? /**?
? ?*?Created?by?JetBrains?PhpStorm.?
? ?*?User:?lsl?
? ?*?Date:?13-8-28?
? ?*?Time:?下午2:58?
? ?*?敏感詞過濾工具類?
? ?*?使用方法?
? ?*?echo?FilterTools::filterContent(&quot;你媽的我操一色狼雜種二山食物&quot;,&quot;*&quot;,DIR.&quot;config/word.txt&quot;,$GLOBALS[&quot;p_memcache&quot;][&quot;bad_words&quot;]);?
? ?*/?
? ?
? class?FilterTools?{?
? ????public?static?$keyword?=?array();?
? ????/**?
? ?????*?從文件中加載敏感詞?
? ?????*?@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?待處理字符串?
? ?????*?@param?$target??替換后的字符?
? ?????*?@param?$filename??敏感詞配置文件?
? ?????*?@param?$memconfig?緩存配置文件?
? ?????*?@return?處理后的字符串?
? ?????*/?
? ????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)));?
? ????}?
? ?
? }?
? ?
? /**?
? ?*?敏感詞緩存處理類?
? ?*?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();?
? ????}?
? ?
? ????/**?
? ?????*?初始化?
? ?????*?@param?bool?$isReset?
? ?????*/?
? ????function?init($isReset?=?false){?
? ????????$this-&gt;list?=?$this-&gt;memcache-&gt;get($this-&gt;key)?$this-&gt;memcache-&gt;get($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');?
? ????????}?
? ????}?
? ?
? ????/**?
? ?????*?獲取列表?
? ?????*?@return?mixed?
? ?????*/?
? ????function?getList(){?
? ????????return?$this-&gt;list;?
? ????}?
? }?

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的php过滤敏感词实例代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。