PHPackages                             lovevivi/hyperf-sensitive-words - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Search &amp; Filtering](/categories/search)
4. /
5. lovevivi/hyperf-sensitive-words

ActiveLibrary[Search &amp; Filtering](/categories/search)

lovevivi/hyperf-sensitive-words
===============================

基于DFA算法的敏感词过滤组件，支持动态白名单、表情符号处理、变形文本检测等高级功能

v1.0.0(11mo ago)12[1 issues](https://github.com/lovevivi/hyperf-sensitive-words/issues)MITPHPPHP &gt;=7.3

Since May 29Pushed 11mo agoCompare

[ Source](https://github.com/lovevivi/hyperf-sensitive-words)[ Packagist](https://packagist.org/packages/lovevivi/hyperf-sensitive-words)[ Docs](https://github.com/lovevivi/hyperf-sensitive-words)[ RSS](/packages/lovevivi-hyperf-sensitive-words/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (9)Versions (2)Used By (0)

Hyperf 敏感词过滤组件
==============

[](#hyperf-敏感词过滤组件)

基于确定有穷自动机算法(DFA)的敏感词过滤 Hyperf 组件，高效、灵活，支持协程环境。支持表情符号处理、变形文本检测和动态白名单管理。

特性亮点
----

[](#特性亮点)

- 🚀 **高性能DFA算法**：基于确定有穷自动机，检测效率高
- 🏗️ **模块化架构**：采用单责任原则和策略模式，代码结构清晰，易于维护和扩展
- 🎯 **智能白名单系统**：支持动态白名单管理，避免误判
- 🔧 **灵活配置**：支持多种表情符号处理策略和变形文本检测
- 💾 **智能缓存**：三级缓存机制，大幅提升加载速度
- 🔍 **前缀索引加速**：针对中等长度文本的匹配优化
- 🌐 **协程友好**：完全支持 Hyperf 协程环境
- 📊 **详细位置信息**：支持返回敏感词的精确位置和长度
- 🛡️ **异常安全**：敏感词处理异常不会影响业务流程
- 🔍 **模糊匹配**：支持绕过技术检测，如表情符号分隔、特殊字符插入等

安装
--

[](#安装)

```
composer require lovevivi/hyperf-sensitive-words
```

发布配置
----

[](#发布配置)

```
php bin/hyperf.php vendor:publish lovevivi/hyperf-sensitive-words
```

配置
--

[](#配置)

配置文件位于 `config/autoload/sensitive_words.php`：

```
return [
    // 用户自定义敏感词库路径，留空则使用默认词库
    'word_path' => '',

    // 词库合并模式：override-覆盖模式，append-追加模式
    'merge_mode' => 'append',

    // 是否开启中间件自动过滤
    'middleware_enable' => false,

    // 替换字符
    'replace_char' => '*',

    // 是否重复替换字符
    'repeat_char' => true,

    // 处理哪些HTTP请求参数
    'http_params' => ['content', 'text', 'message'],

    // 是否启用词库缓存
    'enable_cache' => true,

    // 缓存过期时间（秒），默认86400秒（1天）
    'cache_expire' => 86400,

    // 缓存文件存放路径，留空则按优先级决定
    'cache_path' => '',

    // 是否在应用启动时预热词库
    'preload' => false,

    // 是否启用前缀索引加速
    'enable_prefix_index' => true,

    // 表情符号处理策略 (ignore, remove, replace, include)
    'emoji_strategy' => 'remove',

    // 表情符号替换占位符（当emoji_strategy为replace时有效）
    'emoji_placeholder' => '[表情]',

    // 是否启用变形文本检测（如拼音、特殊字符分隔等）
    'detect_variant_text' => true,

    // 变形文本映射表路径（自定义映射表，留空使用内置映射）
    'variant_map_path' => '',

    // 白名单词库 - 新增功能
    // 这里的词语即使命中敏感词规则，也不会被处理
    'whitelist' => [
        'assessment',       // 示例: 防止 "ass" 被错误匹配
        'helloween',        // 示例: 防止 "hell" 被错误匹配
        '中华人民共和国',   // 示例: 整个词是合法的
        // 在这里添加您需要的白名单词
    ],
];
```

使用方式
----

[](#使用方式)

### 1. 使用服务方式

[](#1-使用服务方式)

```
