PHPackages                             jiangwu10057/dfa-sensitive - 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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. jiangwu10057/dfa-sensitive

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

jiangwu10057/dfa-sensitive
==========================

copy from FireLustre/php-dfa-sensitive,To achieve the filtering of sensitive words, based on the determination of finite automata (DFA) algorithm.

v2.1(5y ago)0171MITPHPPHP &gt;=7.2

Since Jan 12Pushed 5y agoCompare

[ Source](https://github.com/jiangwu10057/php-dfa-sensitive)[ Packagist](https://packagist.org/packages/jiangwu10057/dfa-sensitive)[ RSS](/packages/jiangwu10057-dfa-sensitive/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (3)Versions (7)Used By (0)

 [![Build Status](https://camo.githubusercontent.com/e59b2542a38640600142d5dd986272619856933c6fc65b4dda50a00d58fdf90e/68747470733a2f2f6170692e7472617669732d63692e636f6d2f6a69616e67777531303035372f7068702d6466612d73656e7369746976652e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/jiangwu10057/php-dfa-sensitive) [![Php Version](https://camo.githubusercontent.com/b89e839d5d52995202e3f77713d3272afdfffacf314e3562e9a20a1d070b6ff7/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d2533453d372e322d627269676874677265656e2e7376673f6d61784167653d32353932303030)](https://secure.php.net/)

php-DFA-filterWord
==================

[](#php-dfa-filterword)

php实现基于确定有穷自动机算法的铭感词过滤

安装&amp;使用流程
-----------

[](#安装使用流程)

### Download and install Composer:

[](#download-and-install-composer)

```
curl -sS https://getcomposer.org/installer | php

```

> 要检查 Composer 是否正常工作，只需要通过 php 来执行 PHAR

```
php composer.phar

```

### 安装扩展

[](#安装扩展)

```
composer require jiangwu10057/dfa-sensitive

```

- 注意:如果你在使用composer安装时，出现
    Could not find package jiangwu10057/php-dfa-sensitive at any version for your minimum-stability (stable). Check the package spelling or your minimum-stability 请在你的composer.json中加入`"minimum-stability": "dev"`

#### 如果你需要手动引入

[](#如果你需要手动引入)

```
require './vendor/autoload.php';

use DfaFilter\SensitiveHelper;

```

### 构建敏感词库树

[](#构建敏感词库树)

场景一: 可以拿到不同（用户）词库数组

```
// 获取感词库索引数组
$wordData = array(
    '察象蚂',
    '拆迁灭',
    '车牌隐',
    '成人电',
    '成人卡通',
    ......
);

// get one helper
$handle = SensitiveHelper::init()->setTree($wordData);

```

场景二: 全站使用一套敏感词库

```
// 获取感词库文件路径
$wordFilePath = 'tests/data/words.txt';

// get one helper
$handle = SensitiveHelper::init()->setTreeByFile($wordFilePath);

```

### 设置干扰因子集合

[](#设置干扰因子集合)

> 注意只干扰因子只支持单个字符或单个汉字，暂不支持词

> 但是多个干扰因子连在一起，敏感词可以准确识别

```
$handle = SensitiveHelper::init()->setStopWordList(['&', '*', '.'])->setTreeByFile($wordFilePath);

```

### 忽略大小写

[](#忽略大小写)

> 注意该设置只有在构建敏感词库树之前调用

> 在构建敏感词库树之后调用，结果可能不符合预期

```
$handle = SensitiveHelper::init()->setIgnoreCase()->setTree(['Av', '赌球网'])

```

### 检测是否含有敏感词

[](#检测是否含有敏感词)

```
$islegal = $handle->islegal($content);

```

### 敏感词过滤

[](#敏感词过滤)

```
// 敏感词替换为*为例（会替换为相同字符长度的*）
$filterContent = $handle->replace($content, '*', true);

 // 或敏感词替换为***为例
 $filterContent = $handle->replace($content, '***');

```

### 标记敏感词

[](#标记敏感词)

```
 $markedContent = $handle->mark($content, '', '');

```

### 获取文字中的敏感词

[](#获取文字中的敏感词)

```
// 获取内容中所有的敏感词
$sensitiveWordGroup = $handle->getBadWord($content);
// 仅且获取一个敏感词
$sensitiveWordGroup = $handle->getBadWord($content, 1);

```

#### 使用composer自动加载php命名空间

[](#使用composer自动加载php命名空间)

```
$ composer update
```

### 运行单元测试

[](#运行单元测试)

```
$ composer test
```

注：本仓库fork自：[https://github.com/FireLustre/php-dfa-sensitive，](https://github.com/FireLustre/php-dfa-sensitive%EF%BC%8C) 现仅做个人学习使用！**建议使用原仓库！**

本fork在原有基础上

- 新增两个特性
    - getBadWord返回的敏感词列表是排查的，避免替换敏感词的出现重复替换的问题（提过PR，没有通过所以留个记录！）
    - 新增停止词功能，提高敏感词命中率
    - 新增忽略大小写功能
- ci的集成

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~258 days

Total

5

Last Release

2060d ago

Major Versions

v1.4 → v2.12020-11-10

PHP version history (2 changes)v1.1PHP &gt;=5.3

v2.1PHP &gt;=7.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6183367?v=4)[jiangwu10057](/maintainers/jiangwu10057)[@jiangwu10057](https://github.com/jiangwu10057)

---

Top Contributors

[![FireLustre](https://avatars.githubusercontent.com/u/16452711?v=4)](https://github.com/FireLustre "FireLustre (30 commits)")[![jiangwu10057](https://avatars.githubusercontent.com/u/6183367?v=4)](https://github.com/jiangwu10057 "jiangwu10057 (23 commits)")[![zedisdog](https://avatars.githubusercontent.com/u/8280666?v=4)](https://github.com/zedisdog "zedisdog (6 commits)")[![aaronlei](https://avatars.githubusercontent.com/u/16218247?v=4)](https://github.com/aaronlei "aaronlei (1 commits)")[![guanguans](https://avatars.githubusercontent.com/u/22309277?v=4)](https://github.com/guanguans "guanguans (1 commits)")[![lbool](https://avatars.githubusercontent.com/u/12405248?v=4)](https://github.com/lbool "lbool (1 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/jiangwu10057-dfa-sensitive/health.svg)

```
[![Health](https://phpackages.com/badges/jiangwu10057-dfa-sensitive/health.svg)](https://phpackages.com/packages/jiangwu10057-dfa-sensitive)
```

###  Alternatives

[marcosh/php-validation-dsl

A DSL for validating data in a functional fashion

483.9k](/packages/marcosh-php-validation-dsl)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
