danek/clean-speak
最新稳定版本:v1.0.0
Composer 安装命令:
composer require danek/clean-speak
包简介
PHP profanity filter with leet speak and diacritics detection, customizable dictionaries, and flexible word replacement.
README 文档
README
A PHP library for profanity filtering and text censoring with support for leet speak obfuscation and diacritics.
Contents
Requirements
- PHP 7.1+
Installation
composer require danek/clean-speak
Usage
<?php use CleanSpeak\CleanSpeak; $filter = new CleanSpeak(); $filter->setWords(__DIR__ . '/my_dictionary.json'); // set dictionary from array or JSON $filter->addWords(['badword', 'naughtyword']); // add words to dictionary $filter->setText("Some b4dw0rd and n@ughtyω0rd text!"); // set text echo $filter->filter(); // check // Output: "Some ####### and ########### text!" if (!$filter->isClean()) { echo "Found bad words: " . implode(', ', $filter->getBadWords()); echo "Filtered: " . $filter->filter(); }
Configuration
Word dictionary
<?php use CleanSpeak\CleanSpeak; $filter = new CleanSpeak(); // set/replace dictionary from JSON file $filter->setWords(__DIR__ . '/my_dictionary.json'); // or set/replace dictionary from PHP array $filter->setWords(['word1', 'word2']); // Add to existing dictionary $filter->addWords(['word3', 'word4']);
Blocking Strategies
<?php use CleanSpeak\CleanSpeak; $filter = new CleanSpeak(); $filter->setBlocker('x'); // replacement with random characters $filter->setBlocker(function ($context) { $length = $context['word_length']; $letters = 'abcdefghijklmnopqrstuvwxyz?#+-&!$@%*'; $result = ''; for ($i = 0; $i < $length; $i++) { $result .= $letters[random_int(0, strlen($letters) - 1)]; } return $result; });
Detection Modes
<?php use CleanSpeak\CleanSpeak; $filter = new CleanSpeak(); // Strict mode: Exact word matching only $filter->setStrictMode(true); // Non-strict: Detects leet speak obfuscation $filter->setStrictMode(false); // Strict replacement: Match original word length $filter->setStrictClean(true);
Custom Leet Map
<?php use CleanSpeak\CleanSpeak; $filter = new CleanSpeak(); $filter->setLeetMap([ 'a' => '(a|4|@|\\*)', 's' => '(s|5|\\$|\\*)', // ... more mappings ]); // Or modify existing map $filter->editLeetMap(function($map) { $map['e'] = '(e|3|€|\\*)'; return $map; });
Diacritics Handling
<?php use CleanSpeak\CleanSpeak; $filter = new CleanSpeak(); // Custom diacritics remover $filter->setDiacriticsRemover(function($text) { // use 3rd party library return \Vendor\Slugify::content($text); });
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-07-19