Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Merklin's avatar

Package to check user input against bad words

Laravel Blacklist is a Laravel package for blacklist validation of user input. Includes both system blacklist words and profanity/offensive terms, with the flexibility to choose which lists to use. Uses whole word matching to prevent false positives.

The source config file consist of ~70 blacklist words and ~50 profanity words to check against.

  1. Configuration Options

The package provides three modes for filtering content:

// config/blacklist.php
return [
    // Choose which lists to use: 'blacklist', 'profanity', or 'both'
    'mode' => 'blacklist',
    
    // System blacklist words (usernames, reserved terms, etc.)
    'blacklist' => [
        'admin',
        'system',
        // ...
    ],
    
    // Profanity and offensive terms
    'profanity' => [
        // Common profanity words
        // ...
    ],
];
  1. Usage
use Milenmk\LaravelBlacklist\BlacklistService;

class YourController
{
    protected BlacklistService $blacklistService;
    
    public function __construct(BlacklistService $blacklistService)
    {
        $this->blacklistService = $blacklistService;
    }
    
    public function store(Request $request)
    {
        // Validate request...
        
        // Check fields against blacklisted words
        $blacklistErrors = $this->blacklistService->checkFields([
            'name' => $request->input('name'),
            'email' => $request->input('email'),
            // Add any other fields you want to check
        ]);
        
        if (!empty($blacklistErrors)) {
            return redirect()->back()->withErrors($blacklistErrors);
        }
        
        // Continue with your logic...
    }
}

https://github.com/milenmk/laravel-blacklist

https://packagist.org/packages/milenmk/laravel-blacklist

1 like
1 reply
jlrdw's avatar

Thanks for sharing this.

1 like

Please or to participate in this conversation.