PHP敏感詞處理
阿新 • • 發佈:2020-08-01
composer require lustre/php-dfa-sensitive
use DfaFilter\SensitiveHelper;
/** * 過濾敏感詞 */ public function getSensitiveWord() { $json = new Json(); $red = Red::create(); $wordData = $red->get('tenseven_sensitive_word'); if ($wordData) { $wordData = json_decode($wordData,true); } else { $sensitive_word = M('sensitive_word'); $wordData = $sensitive_word->getField('name',true); $red->setex('tenseven_sensitive_word',60,json_encode($wordData)); } $text = $_POST['text']; $handle = SensitiveHelper::init()->setTree($wordData); $markedContent = $handle->mark($text, '<mark>', '</mark>'); $json->S($markedContent); }
離開事件
$("#text").blur(function(){ var text = $(this).val(); $.ajax({ type:'POST', url:'__APP__/Public/getSensitiveWord', data: {text: text}, dataType:'json', success:function(data){ if(data.errno == 0){ $('#show_sensitive').html(data.data); }else{ alert(data.errdesc); return false; } } }); });
可以用表管理敏感詞,sensitive_word。
CREATE TABLE `tf_sensitive_word` ( `id` INT(11) NOT NULL AUTO_INCREMENT COMMENT 'id' , `name` VARCHAR(50) NOT NULL COMMENT '名稱' , `create_time` INT(11) NOT NULL COMMENT '建立時間' , PRIMARY KEY (`id`), UNIQUE (`name`)) ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_general_ci COMMENT = '抖音敏感詞表';