PHP敏感词处理

composer require lustre/php-dfa-sensitive
use DfaFilterSensitiveHelper;
/**
 * 过滤敏感词
 */
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 = '抖音敏感词表';
原文地址:https://www.cnblogs.com/jiqing9006/p/13415266.html