在php中,可以通过trie_filter扩展实现关键词的过滤,具体操作如下

1.安装libdatrie的依赖库 libiconv

wget https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
tar zxvf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure
make
make install

2. 安装:libdatrie(需要最低版本为0.2.4)

wget ftp://linux.thai.net/pub/ThaiLinux/software/libthai/libdatrie-0.2.4.tar.gz
tar zxf libdatrie-0.2.4.tar.gz
cd libdatrie-0.2.4
./configure --prefix=/usr/local
make
make install

编译出现错误 trietool.c:125: undefined reference to `libiconv'

解决办法为:./configure LDFLAGS=-L/usr/local/lib LIBS=-liconv

3.安装trie_filter 扩展

下载源码包 https://github.com/wulijun/php-ext-trie-filter 在这里下载源码

phpize
./configure --with-php-config=/usr/local/bin/php-config
make
make install

注意:如果是php7的版本扩展源码请移步至https://github.com/zzjin/php-ext-trie-filter/tree/php7

4.修改 php.ini 文件,添加 trie_filter 扩展:extension=trie_filter.so,重启PHP

如何在项目中使用关键词过滤

1、做一个后台来录入关键词,将文件保存在服务器,并且生成相对应的tree文件

public function keywordsAction(){
$file_path = APP_PATH."public/filter/";
if($this->request->isPost()){
$keywords = $this->request->get('keywords','trim');
if(file_put_contents($file_path.'keywords.txt',$keywords)){
$handle = fopen($file_path.'keywords.txt', 'r');
// 生成空的trie-tree-filter
$resTrie = trie_filter_new();
while(! feof($handle)) {
$item = trim(fgets($handle));
if (empty($item)) {
continue;
}
// 把敏感词逐个加入trie-tree
trie_filter_store($resTrie, $item);
}
// 生成trie-tree文件
$blackword_tree = $file_path.'blackword.tree';
trie_filter_save($resTrie, $blackword_tree);
return $this->responseJson();
}
return $this->responseJson(300,'操作失败');
}else{
$type = $this->request->get('type','trim','add');
$keywords = '';
if($type == 'update'){
$keywords = file_get_contents($file_path.'keywords.txt');
}
$this->view->setVar('keywords',$keywords)->pick('Index/keywords');
} }
<?php
class FilterHelper
{ // trie-tree对象
private static $_resTrie = null;
// 字典树的更新时间
private static $_mtime = null; /**
* 防止初始化
*/
private function __construct() {} /**
* 防止克隆对象
*/
private function __clone() {} /**
* 提供trie-tree对象
*
* @param $tree_file 字典树文件路径
* @param $new_mtime 当前调用时字典树的更新时间
* @return null
*/
static public function getResTrie($tree_file, $new_mtime) { if (is_null(self::$_mtime)) {
self::$_mtime = $new_mtime;
} if (($new_mtime != self::$_mtime) || is_null(self::$_resTrie)) {
self::$_resTrie = trie_filter_load($tree_file);
self::$_mtime = $new_mtime;
}
return self::$_resTrie;
} /**
* 过滤替换关键词
* @param $content 原字符串
* @param string $replace_str 替换后的字符
* @return string
*/
static public function filter($content,$replace_str = "*"){
$arrRet = self::isHasKeywords($content);
$badwords = [];
foreach($arrRet as $ret){
$badwords[] = substr($content, $ret[0], $ret[1]);
// $key = substr($content, $ret[0], $ret[1]);
// $content = str_replace($key,str_repeat($replace_str,mb_strlen($key)),$content);
}
return str_replace($badwords,$replace_str,$content);
} /**
* 是否有过滤词
* @param $content
* @return array
*/
static public function isHasKeywords($content){
$arrRet = trie_filter_search_all(self::$_resTrie, $content);
return $arrRet;
}
}

2、在项目相应位置调用关键词过滤替换即可

public function testAction(){
$content = '你果真是个贱人啊';
$tree_file = APP_PATH."public/filter/blackword.tree";
clearstatcache(); // 获取请求时,字典树文件的修改时间
$new_mtime = filemtime($tree_file);
$resTrie = \FilterHelper::getResTrie($tree_file,$new_mtime);
echo \FilterHelper::filter($content); }

以上就是使用php扩展实现关键词过滤

php实现关键词过滤的更多相关文章

  1. Shell 脚本模拟 milter 实现黑白名单及关键词过滤

    程序执行流程:1. 开始接受邮件.2. 检查发件人是否在黑名单内,如果是拒绝接受;否则继续3. 检查发件人是否在白名单内,如果是接收邮件;否则继续4. 对邮件进行关键字过滤,如果邮件中包含被过滤的关键 ...

  2. C#敏感关键词过滤代码

    System.Text.StringBuilder sb = new System.Text.StringBuilder(text.Length);             string filter ...

  3. 高效Java敏感词、关键词过滤工具包_过滤非法词句

    敏感词.文字过滤是一个网站必不可少的功能,如何设计一个好的.高效的过滤算法是非常有必要的.前段时间我一个朋友(马上毕业,接触编程不久)要我帮他看一个文字过滤的东西,它说检索效率非常慢.我把它程序拿过来 ...

  4. Discuz网警过滤关键词库

    积累近几年discuz关键词过滤 使用方法:1.进入后台/内容/词语过滤批量添加.2.打开CensorWords.txt,复制里面的文本信息到批量添加的输入框内,点击确定即可.如图: 关键词下载:Ke ...

  5. php关键词替换的类(避免重复替换,保留与还原原始链接)

    转载:http://www.169it.com/blog_article/601549531.html 本节主要内容:一个关键词替换的类 主要可以用于关键词过滤,或关键词查找替换方面. 实现过程分析: ...

  6. 灵玖软件Nlpir Parser语义智能内容过滤

    Internet是全球信息共享的基础设施,是一种开放和面向 所有用户的技术.它一方面要保证信息方便.快捷的共享;另一方面要防止垃圾信息的传播.网络内容分析是一种管理信 息传播的重要手段.它是网络信息安 ...

  7. [原创] Trie树 php 实现敏感词过滤

    目录 背景 简介 存储结构 PHP 其他语言 字符串分割 示例代码 php 优化 缓存字典树 常驻服务 参考文章 背景 项目中需要过滤用户发送的聊天文本, 由于敏感词有将近2W条, 如果用 str_r ...

  8. java过滤敏感词汇

    前言 现在几乎所有的网站再发布带有文字信息的内容时都会要求过滤掉发动的.不健康的.影响社会安定的等敏感词汇,这里为大家提供了可以是现在这种功能的解决方案 第一种方式 创建敏感词汇文件:首先需要准备一个 ...

  9. ZenCart通过Contact Us接收垃圾邮件的过滤方案

    最近收到一些通过Contact Us进行垃圾外链群发的邮件,虽然可以通过在Contact Us增加验证码来解决,但不利于客户体验.所以我们可以通过简单的关键词过滤来实现,一般垃圾外链都含有“[url= ...

  10. ctfhub 过滤运算符 综合过滤练习 读取源代码 远程包含 eval执行 文件包含 php://input

    过滤运算符 过滤了\和&那么尝试; 成功那么将flag cat出来 127.0.0.1;cat flag_27249269530391.php 得到flag 综合过滤练习 这次过滤有点多过滤了 ...

随机推荐

  1. [python] Python并行计算库Joblib使用指北

    Joblib是用于高效并行计算的Python开源库,其提供了简单易用的内存映射和并行计算的工具,以将任务分发到多个工作进程中.Joblib库特别适合用于需要进行重复计算或大规模数据处理的任务.Jobl ...

  2. 07-canvas绘制虚线

    1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="U ...

  3. 微服务全链路跟踪:springcloud集成jaeger

    微服务全链路跟踪:grpc集成zipkin 微服务全链路跟踪:grpc集成jaeger 微服务全链路跟踪:springcloud集成jaeger 微服务全链路跟踪:jaeger集成istio,并兼容u ...

  4. 快手 内推码:TYORVzmsw 秋招 应届生/实习生 真正本人内推 已有多人在我内推之后,接连顺利通过了HR筛选、用人部门筛选、面试!

    内推码:TYORVzmsw 校园招聘岗位列表:https://campus.kuaishou.cn/#/campus/jobs?code=TYORVzmsw 真正的本人内部推荐! 已有多人在我内推之后 ...

  5. WPF 设备输入事件封装

    本文主要介绍WPF应用对鼠标输入.触摸屏触笔以及触摸事件的封装 之前有简单说明设备输入类型 WPF 屏幕点击的设备类型 - 唐宋元明清2188 - 博客园 (cnblogs.com) 1.鼠标 - 通 ...

  6. Visual studio 2019 无法推送代码到 GitHub

    博客转载:VS2022 无法推送到GitHub,也无法克隆项目_vs连不上github-CSDN博客 问题描述` 使用vs2019 git无法推送到github 解决办法` 按照大神的描述设置,非常好 ...

  7. 效率跃升16倍!火山引擎ByteHouse助力销售数据平台复杂查询效率大幅提高

    更多技术交流.求职机会,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流群.   销售数据,是反映市场趋势.消费者行为以及产品表现的重要指标,也是企业做出精准决策的关键依据.因此,对销售数据 ...

  8. 2024 秋季PAT认证甲级(题解A1-A4)

    2024 秋季PAT认证甲级(题解A-D) 写在前面 这一次PAT甲级应该是最近几次最简单的一次了,3个小时的比赛差不多30分钟就ak了(也是拿下了整场比赛的rk1),下面是题解报告,每个题目差不多都 ...

  9. Round #2022/11/26

    问题 B:染色 题目描述 有长度为 \(n\) 的一个序列,编号为 \(1\) 到 \(n\) ,现要对这些元素进行染色标记,若编号 \(i-j\) 为素数,且 \(1\le i < j \le ...

  10. TypeScript – Using Disposable

    前言 TypeScript v5.2 多了一个新功能叫 Disposable. Dispose 的作用是让 "对象" 离开 "作用域" 后做出一些 " ...