Blasp is a profanity filter package for Laravel that helps detect and mask profane words in a given sentence. It offers a robust set of features for handl
Blasp is a profanity filter package for Laravel that helps detect and mask profane words in a given sentence. It offers a robust set of features for handl
Blasp is a powerful, extensible profanity filter for Laravel. Version 4 is a ground-up rewrite with a driver-based architecture, severity scoring, masking strategies, Eloquent model integration, and a clean fluent API.
regex (detects obfuscation, substitutions, separators), pattern (fast exact matching), phonetic (catches sound-alike evasions), or pipeline (chains multiple drivers together). Extend with custom drivers.*, #), grawlix (!@#$%), or a custom callback.Blaspable trait auto-sanitizes or rejects profanity on model save.Blasp::fake() for test doubles with assertions.ProfanityDetected, ContentBlocked, and ModelProfanityDetected.composer require blaspsoft/blaspPublish configuration:
# Everything (config + language files)
php artisan vendor:publish --tag="blasp"
# Config only
php artisan vendor:publish --tag="blasp-config"
# Language files only
php artisan vendor:publish --tag="blasp-languages"use Blaspsoft\Blasp\Facades\Blasp;
$result = Blasp::check('This is a fucking sentence');
$result->isOffensive(); // true
$result->clean(); // "This is a ******* sentence"
$result->original(); // "This is a fucking sentence"
$result->score(); // 30
$result->count(); // 1
$result->uniqueWords(); // ['fucking']
$result->severity(); // Severity::HighAll builder methods return a PendingCheck and can be chained:
…The Result object is returned by every check() call:
| Method | Returns | Description |
|---|---|---|
isOffensive() |
bool |
Text contains profanity |
isClean() |
bool |
Text is clean |
clean() |
string |
Text with profanities masked |
original() |
string |
Original unmodified text |
score() |
int |
Severity score (0-100) |
count() |
int |
Total profanity matches |
uniqueWords() |
array |
Unique base words detected |
severity() |
?Severity |
Highest severity in matches |
words() |
Collection |
MatchedWord objects with position, length, severity |
toArray() |
array |
Full result as array |
toJson() |
string |
Full result as JSON |
Result implements JsonSerializable, Stringable (returns clean text), and Countable.
The regex driver detects obfuscated profanity:
| Type | Example | Detected As |
|---|---|---|
| Straight match | fucking |
fucking |
| Substitution | fÛck!ng, f4ck |
fucking, fuck |
| Separators | f-u-c-k-i-n-g, f@ck |
fucking, fuck |
| Doubled | ffuucckkiinngg |
fucking |
| Combination | f-uuck!ng |
fucking |
Separator limit: The regex driver allows up to 3 separator characters between each letter (e.g.,
f--u--c--k). This covers all realistic obfuscation patterns while keeping regex complexity low enough for PHP-FPM environments.
The pattern driver only detects straight word-boundary matches.
The phonetic driver uses metaphone() + Levenshtein distance to catch words that sound like profanity but are spelled differently:
| Type | Example | Detected As |
|---|---|---|
| Phonetic spelling | phuck |
fuck |
| Shortened form | fuk |
fuck |
| Sound-alike | sheit |
shit |
Configure sensitivity in config/blasp.php under drivers.phonetic. A curated false-positive list prevents common words like "fork", "duck", and "beach" from being flagged.
The pipeline driver chains multiple drivers together so a single check() call runs all of them. It uses union merge semantics — text is flagged if any driver finds a match.
// Config-based: set 'default' => 'pipeline' or use driver('pipeline')
Blasp::driver('pipeline')->check('phuck this sh1t');
// Ad-hoc: pick drivers on the fly (no config needed)
Blasp::pipeline('regex', 'phonetic')->check('phuck this sh1t');
Blasp::pipeline('regex', 'pattern', 'phonetic')->check($text);When multiple drivers detect the same word at the same position, duplicates are removed — only the longest match is kept. Masks are applied from the merged result, and the score is recalculated across all matches.
Configure the default sub-drivers in config/blasp.php:
'drivers' => [
'pipeline' => [
'drivers' => ['regex', 'phonetic'], // Drivers to chain
],
],The Blaspable trait automatically checks model attributes during save:
use Blaspsoft\Blasp\Blaspable;
class Comment extends Model
{
use Blaspable;
protected array $blaspable = ['body', 'title'];
}// Sanitize mode (default) — profanity is masked, model saves
$comment = Comment::create(['body' => 'This is fucking great']);
$comment->body; // "This is ******* great"
// Check what happened
$comment->hadProfanity(); // true
$comment->blaspResults(); // ['body' => Result, 'title' => Result]
$comment->blaspResult('body'); // Result instanceclass Comment extends Model
{
use Blaspable;
protected array $blaspable = ['body', 'title'];
protected string $blaspMode = 'reject'; // 'sanitize' (default) | 'reject'
protected string $blaspLanguage = 'spanish'; // null = config default
protected string $blaspMask = '#'; // null = config default
}In reject mode, saving a model with profanity throws ProfanityRejectedException and the model is not persisted:
use Blaspsoft\Blasp\Exceptions\ProfanityRejectedException;
try {
$comment = Comment::create(['body' => 'profane text']);
} catch (ProfanityRejectedException $e) {
$e->attribute; // 'body'
$e->result; // Result instance
$e->model; // The unsaved model
}Comment::withoutBlaspChecking(function () {
Comment::create(['body' => 'unchecked content']);
});A ModelProfanityDetected event fires whenever profanity is detected on a model attribute (both sanitize and reject modes):
use Blaspsoft\Blasp\Events\ModelProfanityDetected;
Event::listen(ModelProfanityDetected::class, function ($event) {
$event->model; // The model instance
$event->attribute; // Which attribute had profanity
$event->result; // Result instance
});Use CheckProfanity to filter incoming request fields. A blasp middleware alias is registered automatically:
// Using the short alias (recommended)
Route::post('/comment', CommentController::class)
->middleware('blasp');
// With parameters: action, severity
Route::post('/comment', CommentController::class)
->middleware('blasp:sanitize,mild');
// Or using the class directly
use Blaspsoft\Blasp\Middleware\CheckProfanity;
Route::post('/comment', CommentController::class)
->middleware(CheckProfanity::class);| Action | Behaviour |
|---|---|
reject (default) |
Returns 422 JSON with field errors |
sanitize |
Replaces profane fields in the request and continues |
Configure which fields to check in config/blasp.php:
'middleware' => [
'action' => 'reject',
'fields' => ['*'], // '*' = all fields
'except' => ['password', 'email', '_token'], // Always skipped
'severity' => 'mild',
],$request->validate([
'comment' => ['required', 'blasp_check'],
'bio' => ['required', 'blasp_check:spanish'],
]);use Blaspsoft\Blasp\Rules\Profanity;
use Blaspsoft\Blasp\Enums\Severity;
$request->validate([
'comment' => ['required', Profanity::in('english')],
'bio' => ['required', Profanity::severity(Severity::High)],
'tagline' => ['required', Profanity::maxScore(50)],
]);The @clean directive sanitizes and escapes text for safe display in views:
@clean($comment->body)
{{-- Equivalent to: {{ app('blasp')->check($comment->body)->clean() }} --}}Output is HTML-escaped via e() for XSS safety.
Blasp registers macros on Laravel's Str and Stringable classes:
use Illuminate\Support\Str;
// Static methods
Str::isProfane('fuck this'); // true
Str::isProfane('hello'); // false
Str::cleanProfanity('fuck this'); // '**** this'
Str::cleanProfanity('hello'); // 'hello'
// Fluent Stringable methods
Str::of('fuck this')->isProfane(); // true
Str::of('fuck this')->cleanProfanity(); // Stringable('**** this')
Str::of('hello')->cleanProfanity()->upper(); // 'HELLO' (chaining works)Full config/blasp.php reference:
…Implement DriverInterface and register with the manager:
use Blaspsoft\Blasp\Core\Contracts\DriverInterface;
use Blaspsoft\Blasp\Core\Result;
use Blaspsoft\Blasp\Core\Dictionary;
use Blaspsoft\Blasp\Core\Contracts\MaskStrategyInterface;
class MyDriver implements DriverInterface
{
public function detect(string $text, Dictionary $dictionary, MaskStrategyInterface $mask, array $options = []): Result
{
// Your detection logic
}
}
// Register in a service provider
Blasp::extend('my-driver', fn($app) => new MyDriver());
// Use it
Blasp::driver('my-driver')->check($text);Blasp caches check() results by default. When the same text is checked with the same configuration (language, driver, severity, allow/block lists), the cached result is returned instantly.
// First call — runs full analysis, caches result
$result = Blasp::check('some text');
// Second call — returns cached result
$result = Blasp::check('some text');Configure caching in config/blasp.php:
'cache' => [
'enabled' => true, // Master switch for all caching
'driver' => env('BLASP_CACHE_DRIVER'), // null = default cache driver
'ttl' => 86400, // Cache lifetime in seconds
'results' => true, // Cache check() results (disable independently)
],Result caching is automatically bypassed when using a CallbackMask (closures can't be serialized). Clear both dictionary and result caches with:
php artisan blasp:clearOr programmatically:
Dictionary::clearCache();# Clear the profanity cache
php artisan blasp:clear
# Test text from the command line
php artisan blasp:test "some text to check" --lang=english --detail
# List available languages with word counts
php artisan blasp:languagesuse Blaspsoft\Blasp\Facades\Blasp;
use Blaspsoft\Blasp\Core\Result;
// Replace with a fake — all checks return clean by default
Blasp::fake();
// Pre-configure specific responses
Blasp::fake([
'bad text' => Result::withMatches(['fuck']),
'clean text' => Result::none('clean text'),
]);
$result = Blasp::check('bad text');
$result->isOffensive(); // true
// Assertions
Blasp::assertChecked();
Blasp::assertCheckedTimes(1);
Blasp::assertCheckedWith('bad text');Blasp::withoutFiltering(function () {
// All checks returNo open issues yet, or sync has not completed.