PHPackages                             bestjf/php-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. bestjf/php-dfa-sensitive

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

bestjf/php-dfa-sensitive
========================

To achieve the filtering of sensitive words, based on the determination of finite automata (DFA) algorithm.

v1.5(4y ago)0362MITPHPPHP &gt;=5.3

Since Jan 12Pushed 4y agoCompare

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

READMEChangelog (1)Dependencies (1)Versions (6)Used By (0)

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 bestjf/php-dfa-sensitive

```

- 注意:如果你在使用composer安装时，出现
    Could not find package bestjf/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);

```

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

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

```
$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);

```

### 如何使用单元测试进行测试

[](#如何使用单元测试进行测试)

#### 安装PHPUnit

[](#安装phpunit)

```
$ wget https://phar.phpunit.de/phpunit.phar

$ chmod +x phpunit.phar

$ mv phpunit.phar /usr/local/bin/phpunit
```

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

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

```
$ composer update
```

### 运行单元测试

[](#运行单元测试)

```
$ phpunit tests/BaseTest.php
```

*如果大家有更好的建议，请大家多多指正，O(∩\_∩)O谢谢*

*你们的star是我的动力*

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 72.1% of commits — single point of failure

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 ~307 days

Total

5

Last Release

1815d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3b35bf261104426b3b0ff21bbde3705c7e22e7e88faeedb13718c47e1cc94e48?d=identicon)[BestJf](/maintainers/BestJf)

---

Top Contributors

[![FireLustre](https://avatars.githubusercontent.com/u/16452711?v=4)](https://github.com/FireLustre "FireLustre (31 commits)")[![zedisdog](https://avatars.githubusercontent.com/u/8280666?v=4)](https://github.com/zedisdog "zedisdog (6 commits)")[![bestjf](https://avatars.githubusercontent.com/u/19410376?v=4)](https://github.com/bestjf "bestjf (3 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

### Embed Badge

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

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

###  Alternatives

[webmozart/assert

Assertions to validate method input/output with nice error messages.

7.6k894.0M1.2k](/packages/webmozart-assert)[bensampo/laravel-enum

Simple, extensible and powerful enumeration implementation for Laravel.

2.0k15.9M104](/packages/bensampo-laravel-enum)[swaggest/json-schema

High definition PHP structures with JSON-schema based validation

48612.5M73](/packages/swaggest-json-schema)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5325.6M19](/packages/stevebauman-purify)[ashallendesign/laravel-config-validator

A package for validating your Laravel app's config.

217905.3k5](/packages/ashallendesign-laravel-config-validator)[crazybooot/base64-validation

Laravel validators for base64 encoded files

1341.9M8](/packages/crazybooot-base64-validation)

PHPackages © 2026

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