diego-ninja/banthis
Composer 安装命令:
composer require diego-ninja/banthis
包简介
A censor and word filtering library for PHP 8.2+
README 文档
README
BanThis is a PHP package for profanity filtering. The PHP script uses regex to intelligently look for "leetspeak"-style numeric or symbol replacements.
This package is an evolution of snipe/banbuilder adapted and refactored to modern php versions.
📦 Installation
To install BanThis, simply include it in your projects's composer.json.
"diego-ninja/banthis": "^1",
There are no additional dependencies required for this package to work.
🚀 Usage
use Ninja\BanThis\Censor; use Ninja\BanThis\Dictionary; $dictionary = Dictionary::withLanguage('en-us'); $censor = new Censor($dictionary); $string = $censor->clean('A very offensive string with the bad word dick on it'); print_r($string) Array ( [orig] => A very offensive string with the bad word dick on it [clean] => A very offensive string with the bad word **** on it [matched] => Array ( [0] => dick ) )
Setting the Dictionary
You can set or add dictionaries to the Censor instance.
// Set a new dictionary $censor->setDictionary($dictionary); // Add words from another dictionary $additionalDictionary = Dictionary::withLanguage('fr'); $censor->addDictionary($additionalDictionary);
Adding words from an array
You can add words directly from an array.
$words = ['badword1', 'badword2']; $censor->addWords($words);
Managing the whitelist
You can add words to the whitelist to exclude them from being censored.
$whitelist = ['goodword1', 'goodword2']; $censor->whitelist($whitelist);
Setting the replacement character
You can set the character or string that will replace the censored words.
$censor->setReplaceChar('*');
⚙️ How it works
In a nutshell, this code takes an array of bad words and compares it to an array of common filter-evasion tactics. It then does a string replacement to insert regex parameters into your badwords array, and then evaluates your input string to that expanded banned word list.
So in your bad words array, you might have:
[0] => 'ass'
The preg_replace functions replace all of the possible shenanigan letters with regex patterns (in lieu of adding the variants onto the end of the array), so the 'ass' in your array gets turned into this, right before the preg_replace checks for matches:
[0] => /(a|a\.|a\-|4|@|Á|á|À|Â|à|Â|â|Ä|ä|Ã|ã|Å|å|α)(s|s\.|s\-|5|\$|§)(s|s\.|s\-|5|\$|§)/i
This means that a word can have none, one or any variety of leet replacements and it will still trip the trigger. Part of the leet filter includes stripping out letter-dash and letter-dots.
This means that the following all evaluate to the "bitch":
- B1tch
- bi7tch
- b.i.t.c.h.
- b-i-t-c-h
- b.1.t.c.h.
- ßitch
- and so on....
🔬 Tests
To run the unit tests on this package, run vendor/bin/phpunit from the package directory.
🙏 Credits
This project is developed and maintained by 🥷 Diego Rin in his free time.
Special thanks to:
- snipe for developing the inital code that serves BanThis as starting point.
- All the contributors and testers who have helped to improve this project through their contributions.
If you find this project useful, please consider giving it a ⭐ on GitHub!
diego-ninja/banthis 适用场景与选型建议
diego-ninja/banthis 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 228 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 10 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「filter」 「language」 「strings」 「profanity」 「swear」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 diego-ninja/banthis 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 diego-ninja/banthis 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 diego-ninja/banthis 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
Easy to use i18n translation PHP class for multi-language websites
Package for convenient work with Laravel's localization features
Store your language lines in the database, yaml or other sources
Nova searchable filter for belongsTo relationships.
Set Links with a specific language parameter
统计信息
- 总下载量: 228
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: AGPL-3.0
- 更新时间: 2024-10-23