waad/laravel-profanity-filter
Installation
composer require waad/laravel-profanity-filter
Publish the config file (if needed):
php artisan vendor:publish --provider="Waad\ProfanityFilter\ProfanityFilterServiceProvider"
Basic Usage Filter text in a controller or blade view:
use Waad\ProfanityFilter\Facades\ProfanityFilter;
$cleanText = ProfanityFilter::filter('This is a bad word!');
// Output: "This is a *** word!"
Configuration
Check config/profanity-filter.php for default settings (e.g., mask, allowed_words, languages).
Filter User-Generated Content
// In a CommentController
public function store(Request $request) {
$validated = $request->validate(['content' => 'required|string']);
$cleanContent = ProfanityFilter::filter($validated['content']);
Comment::create(['content' => $cleanContent]);
}
Filtering Text
// Basic filter
ProfanityFilter::filter('H3ll0 w0rld!');
// Custom mask (e.g., replace with "****")
ProfanityFilter::setMask('****')->filter('badword');
// Case sensitivity
ProfanityFilter::setCaseSensitive(true)->filter('BadWord');
Language Support
// Switch languages dynamically
ProfanityFilter::setLanguage('fr')->filter('Merde!');
// Output: "****!"
Custom Word Lists
// Add custom words
ProfanityFilter::addWords(['customword1', 'customword2']);
ProfanityFilter::filter('This is customword1');
Validation Integration
use Illuminate\Validation\Rule;
$request->validate([
'bio' => [
'required',
Rule::function('filter', function ($attribute, $value, $fail) {
if (ProfanityFilter::containsProfanity($value)) {
$fail('Profanity detected.');
}
}),
],
]);
Middleware for API/Forms Create middleware to auto-filter incoming requests:
// app/Http/Middleware/FilterProfanity.php
public function handle($request, Closure $next) {
$request->merge([
'clean_content' => ProfanityFilter::filter($request->content)
]);
return $next($request);
}
Blade Directives Add a Blade directive for frontend filtering:
// In a service provider
Blade::directive('filter', function ($expression) {
return "<?php echo Waad\ProfanityFilter\Facades\ProfanityFilter::filter($expression); ?>";
});
Usage:
@filter($user->comment)
Queue Delayed Filtering For performance, defer filtering to a queue job:
// Dispatch after saving raw data
FilterProfanityJob::dispatch($user->id);
False Positives
ProfanityFilter::addAllowedWords(['badminton']) or adjust the word list.Leet Speak Detection
config/profanity-filter.php:
'leet_speak' => [
'h' => ['3', '4', 'a', 'e'],
'e' => ['3'],
// ...
],
Performance with Large Texts
$text = str_split($longText, 1000);
array_map([ProfanityFilter::class, 'filter'], $text);
Case Sensitivity Quirks
ProfanityFilter::setCaseSensitive(true);
Check Filtered Output
Use ProfanityFilter::getFilteredText() to inspect replacements:
$filtered = ProfanityFilter::filter('test');
dd(ProfanityFilter::getFilteredText());
Log Detected Profanity Override the filter method to log matches:
ProfanityFilter::extend(function ($text) {
$matches = ProfanityFilter::getDetectedWords();
\Log::info('Profanity detected:', $matches);
return $text;
});
Test Edge Cases
Custom Filter Strategies Replace the default filter with a closure:
ProfanityFilter::setFilterStrategy(function ($text, $words) {
return str_replace($words, 'CENSORED', $text);
});
Database-Backed Word Lists Fetch words dynamically from a DB table:
$customWords = DB::table('profanity_words')->pluck('word')->toArray();
ProfanityFilter::addWords($customWords);
API Wrapper for External Services Integrate with external APIs (e.g., PerspectAPI) for advanced moderation:
ProfanityFilter::extend(function ($text) {
$externalCheck = ExternalApi::check($text);
return $externalCheck['safe'] ? $text : ProfanityFilter::filter($text);
});
Event-Based Filtering Trigger events before/after filtering:
ProfanityFilter::listen('beforeFilter', function ($text) {
// Pre-process text
});
ProfanityFilter::listen('afterFilter', function ($text) {
// Post-process text
});
How can I help you explore Laravel packages today?