PHPackages                             singiu/wordban - 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. singiu/wordban

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

singiu/wordban
==============

简单敏感词过滤组件，采用 DFA 算法。

1.1.1(5y ago)1213.8k↓16.7%1[1 PRs](https://github.com/Singiu/wordban/pulls)MITPHPCI passing

Since Mar 29Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/Singiu/wordban)[ Packagist](https://packagist.org/packages/singiu/wordban)[ RSS](/packages/singiu-wordban/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (1)Versions (7)Used By (0)

简单敏感词过滤组件，采用 DFA 算法。
====================

[](#简单敏感词过滤组件采用-dfa-算法)

[![Latest Stable Version](https://camo.githubusercontent.com/9871a6614ce264590ff7693aafcb3d63a02902fe67850726588bde2fc28bad8d/68747470733a2f2f706f7365722e707567782e6f72672f73696e6769752f776f726462616e2f762f737461626c65)](https://packagist.org/packages/singiu/wordban)[![Total Downloads](https://camo.githubusercontent.com/1d1948bf4d806431bbfa0560c44fb186f92fec48779e2a759d7b4c4f1bd2a704/68747470733a2f2f706f7365722e707567782e6f72672f73696e6769752f776f726462616e2f646f776e6c6f616473)](https://packagist.org/packages/singiu/wordban)[![Latest Unstable Version](https://camo.githubusercontent.com/506ca10354b80576eeb0b5586c192715588fcf4f86e3f4735082d23f376c7840/68747470733a2f2f706f7365722e707567782e6f72672f73696e6769752f776f726462616e2f762f756e737461626c65)](https://packagist.org/packages/singiu/wordban)[![License](https://camo.githubusercontent.com/1439a459c4ea12bad09a57aafd3c0f8f8c83c8ea353adf59035cbf0bc6dfed35/68747470733a2f2f706f7365722e707567782e6f72672f73696e6769752f776f726462616e2f6c6963656e7365)](https://packagist.org/packages/singiu/wordban)

我们的应用往往要营造良好的交流氛围，或因一些政策上的原因，我们需要限制输出用户的某些输入内容（我们称之为敏感词），但用户的输入是不确定的，一般的我们都会对用户的输入进行二次处理，将一些敏感词进行过滤替换的操作。 如果你也有这方面的需要的话，那这个包可以很方便的实现这一功能。

基本用法
----

[](#基本用法)

### 替换操作

[](#替换操作)

首先你需要构建一个敏感词库，这个包中没有另外提供，你需要根据你自己的需要来创建。你可以选择保存在文件中，也可以存入到数据库中，但最终你需要将它们编成一个数组，类似这样：

```
$sensitive_words = array(
  'SB', '傻逼', 'Fuck'
);
```

然后你就可以像这样简单的使用：

```
use Singiu\WordBan;

$text = 'SB就是傻逼！fuck is a bad word!';

WordBan::load($sensitive_words);
$result = WordBan::escape($text); // escape 方法会将文本中找到的敏感词使用替代词（默认是*）替换掉。
echo $result; // **就是**！**** is a bad word!

// 你也可以改变默认的替换字符，如换成 'x'。
WordBan::setEscapeChar('x');
echo WordBan::escape($text); // xx就是xx！xxxx is a bad word!
```

### 检测操作

[](#检测操作)

在某些情况下，我们不需要替换文本中的敏感词，只需要程序检测出是否有敏感词即可。 比如在用户注册时候填写的昵称，为了防止用户冒充官方人员对用户进行诈骗，一般会设置一些不能注册的昵称作为敏感词。 这时我们可以使用 scan 方法，它会返回一个数组，包含所有被检测的到敏感词：

```
use Singiu\WordBan;

$username = $_POST['username'];

$sensitive_words = ['Singiu'];
WordBan::load($sensitive_words);
$bad_words = WordBan::scan($username);

if (count($bad_words) > 0) {
  echo '昵称中含有不合适的字符！';
}
```

关于性能问题
------

[](#关于性能问题)

本包采用 DFA 算法来进行敏感词的查找，我自己的项目实测有 1000+ 的敏感词，查找和替换速度都保持在毫秒级。

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE) for more information.

###  Health Score

44

—

FairBetter than 92% of packages

Maintenance53

Moderate activity, may be stable

Popularity32

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~236 days

Total

5

Last Release

2029d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/68110d35506ef416f842839b6c94aae0259a6b81c676e27f903da9052d5a68d4?d=identicon)[Singiu](/maintainers/Singiu)

---

Top Contributors

[![Singiu](https://avatars.githubusercontent.com/u/3905448?v=4)](https://github.com/Singiu "Singiu (11 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/singiu-wordban/health.svg)

```
[![Health](https://phpackages.com/badges/singiu-wordban/health.svg)](https://phpackages.com/packages/singiu-wordban)
```

###  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)
