waad / laravel-profanity-filter
Laravel Profanity Filter - Powerful PHP package for detecting, filtering, and masking profanity in multiple languages. Supports leet speak, custom word lists, case sensitivity, and seamless Laravel integration.
Requires
- php: ^8.0
- illuminate/support: ^8.0|^9.0|^10.0|^11.0|^12.0
Requires (Dev)
- laravel/pint: ^1.0
- orchestra/testbench: ^7.0|^8.0|^9.0|^10.0
- phpunit/phpunit: ^9.0|^10.0|^11.0|^12.0
README
Laravel Profanity Filter
A powerful, flexible, and easy-to-use PHP Laravel package for detecting, filtering, and masking profanity in multiple languages. Includes advanced features such as leet speak detection, custom word lists, language auto-detection, and real-time configuration.
🚀 Features
- Multi-language support: Detects profanity in English, French, Arabic, and more
- Customizable masking: Replace profane words with your own masking character(s)
- Leet speak & substitutions: Detects obfuscated words (e.g., "f@ck", "sh!t", "d4mn")
- Custom word lists: Add or override profane words per language
- Case sensitivity: Toggle case-sensitive or insensitive detection
- Separator handling: Detects words with separators (e.g., d-a-m-n, s_h_i_t)
- Laravel integration: Seamless usage via Facade, Service Provider, and config
- Real-time configuration: Update settings and word lists at runtime
- Extensible: Easily add new languages, separators, or substitutions
🛠 Requirements
PHP 8.0+
Laravel 8+
Installation
Via Composer
composer require waad/laravel-profanity-filter
Publish the configuration file:
php artisan vendor:publish --tag="profanity-filter"
Publish the words (Optional
) ⚠️:
php artisan vendor:publish --tag="profanity-words"
Configuration
You can configure the package by editing the profanity-filter.php
file in the config
directory.
Usage
Using hasProfanity
method
use Waad\ProfanityFilter\Facades\ProfanityFilter; $text = "This is a test string with some profanity like fuck and shit."; $hasProfanity = ProfanityFilter::hasProfanity($text); echo $hasProfanity; // true
Using filter
method
use Waad\ProfanityFilter\Facades\ProfanityFilter; $text = "This is a test string with some profanity like fuck and shit."; $filteredText = ProfanityFilter::filter($text); echo $filteredText; // This is a test string with some profanity like **** and ****.
Using getProfanityWords
method
use Waad\ProfanityFilter\Facades\ProfanityFilter; $text = "This is a test string with some profanity like fuck and shit."; $profanityWords = ProfanityFilter::getProfanityWords($text); print_r($profanityWords); // Output: Array ( [0] => fuck [1] => shit )
Using setLanguage
method
use Waad\ProfanityFilter\Facades\ProfanityFilter; ProfanityFilter::setLanguage('en'); // default is null (auto detect language) $text = "This is a test string with some profanity like fuck and shit."; $filteredText = ProfanityFilter::filter($text); echo $filteredText; // This is a test string with some profanity like **** and ****.
Using setCaseSensitive
method
use Waad\ProfanityFilter\Facades\ProfanityFilter; ProfanityFilter::setCaseSensitive(true); // default is false $text = "This is a test string with some profanity like fuck and Fuck."; $filteredText = ProfanityFilter::filter($text); echo $filteredText; // This is a test string with some profanity like **** and Fuck.
Using setDetectLeetSpeak
method
use Waad\ProfanityFilter\Facades\ProfanityFilter; ProfanityFilter::setDetectLeetSpeak(true); // default is true $text = "This is a test string with some profanity like f@ck and sh!t."; $filteredText = ProfanityFilter::filter($text); echo $filteredText; // This is a test string with some profanity like **** and ****.
Using setConfig
method
You can update the package configuration in real time using Laravel's config()
helper, or by editing the profanity-filter.php
file in the config
directory.
use Waad\ProfanityFilter\Facades\ProfanityFilter; config(['profanity-filter.custom_words.en' => ['custom']]); ProfanityFilter::setConfig(config('profanity-filter')); $text = "This is a test string with some profanity like fuck and shit."; $filteredText = ProfanityFilter::filter($text); echo $filteredText; // This is a test string with some profanity like **** and ****.
Example All Methods
use Waad\ProfanityFilter\Facades\ProfanityFilter; $text = "This is a test string with some profanity like f@ck and sh!t."; ProfanityFilter::setLanguage('en') ->setDetectLeetSpeak(true) ->setCaseSensitive(false); ProfanityFilter::hasProfanity($text); // true ProfanityFilter::filter($text); // This is a test string with some profanity like *@** and **!*.
Contributing
Contributions are welcome! Please feel free to submit a pull request.
Testing
To run the tests, you can use the following command:
composer test
To Do
- Add support for multiple languages
- Add support for custom words
- Add support for leet speak
- Add support for case sensitivity
- Add support for separators
- Add support for custom replacements
- Add support for custom separators
- Add support for custom substitutions
- Add support for custom word lists
- Add support for custom word lists per language
- Add support for custom word lists in real time
- Add support for custom word from files (json, txt)
License
This package is open-sourced software licensed under the MIT license.