gacbaluyot / profanity-filter
A lightweight, multilingual profanity detection and censorship library written in native PHP
Requires
- php: >=8.0
Requires (Dev)
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2026-03-28 18:59:26 UTC
README
A lightweight, multilingual profanity detection and censorship library written in native PHP.
Supports dynamic language loading, custom word lists, and Unicode-safe pattern matching.
๐ Features
- Detects profanity in text using prebuilt JSON wordlists
- Supports multiple languages:
- English (
en) - Russian (
ru) - Italian (
it) - Indonesian (
id) - Chinese (
zh) - Japanese (
ja)
- English (
- Add or override custom bad words
- Merge multiple language filters or use all at once
- Extract, censor, or save profane words
- PHPUnit test suite included
๐ฆ Installation
Clone the repo and include via Composer:
composer install
Make sure your autoloader can resolve ProfanityFilter\ProfanityFilter from /src.
๐ Usage
Basic Detection
use ProfanityFilter\ProfanityFilter; $filter = new ProfanityFilter('en'); $text = "You idiot!"; if ($filter->containsProfanity($text)) { echo "Profanity detected!"; }
Censor Words
$censored = $filter->censor("You dumb bastard!"); echo $censored; // You **** *******!
Get Detected Words
$words = $filter->getProfaneWords("That was stupid and shitty."); print_r($words); // ['stupid', 'shitty']
Use Multiple Languages
$filter->setLanguages(['en', 'ru']);
Use All Languages
$filter->useAllLanguages();
Add Custom Words
$filter->addCustomWords(['meanie', 'dummy']);
Save & Load Custom Word List
$filter->saveUpdatedWordList(__DIR__ . '/my-words.json'); $filter->setWordListFromFile(__DIR__ . '/my-words.json');
๐งช Running Tests
vendor/bin/phpunit tests/
Make sure youโve run:
composer dump-autoload
to register autoload-dev.
๐ Language Wordlist Files
Each language is stored in a separate JSON file in src/data/, e.g.:
src/data/en.json
src/data/ru.json
src/data/zh.json
You can extend these manually or via the library methods.
๐ License
MIT Licensed. Use at your own discretion โ offensive content warning applies.
๐ Contributions Welcome!
Feel free to fork, submit PRs, or suggest new languages and improvements!