blaspsoft/blasp
Advanced profanity filtering for Laravel with driver-based detection (regex/pattern/phonetic/pipeline), multi-language support, severity scoring (0–100), configurable masking, Eloquent trait auto-sanitizing, middleware and validation rules, plus events and testing fakes.
composer require blaspsoft/blasp and publish config with php artisan vendor:publish --tag="blasp".use Blaspsoft\Blasp\Facades\Blasp;
$result = Blasp::check('This is a fucking sentence');
echo $result->clean(); // "This is a ******* sentence"
config/blasp.php (configuration)app/Providers/BlaspServiceProvider.php (custom drivers, if needed)app/Models/YourModel.php (for Eloquent integration)$cleanText = Blasp::mask('#')->check($userInput)->clean();
$results = Blasp::checkMany(['text1', 'text2']);
foreach ($results as $result) {
echo $result->clean();
}
Blaspable trait to models:
class Comment extends Model {
use Blaspsoft\Blasp\Blaspable;
protected $blaspable = ['body', 'title'];
}
class Comment extends Model {
use Blaspsoft\Blasp\Blaspable;
protected $blaspMode = 'reject';
}
Route::post('/comment', CommentController::class)
->middleware('blasp:sanitize,high');
config/blasp.php:
'middleware' => [
'fields' => ['comment', 'title'],
'except' => ['password'],
],
Profanity rule:
use Blaspsoft\Blasp\Rules\Profanity;
$request->validate([
'bio' => ['required', Profanity::in('spanish')->maxScore(30)],
]);
<p>@clean($comment->body)</p>
Str helpers:
$cleanText = Str::cleanProfanity($userInput);
if (Str::isProfane($userInput)) { ... }
class CustomDriver implements DriverInterface {
public function check(string $text, Dictionary $dictionary): Result {
// Custom logic
}
}
Register it in BlaspServiceProvider:
public function boot() {
Blasp::extend('custom', function () {
return new CustomDriver();
});
}
Performance with Regex Driver:
regex driver is thorough but slower. Use pattern for speed or pipeline for balanced performance.Blasp::cacheResults(true)->check($text);
False Positives in Phonetic Driver:
phonetic driver may flag words like "fork" or "duck." Add them to config/blasp.php under drivers.phonetic.false_positives.Severity Mismatches:
Severity::High may block too much in a casual forum but fit a professional setting.Eloquent Trait Conflicts:
Blaspable, ensure $blaspable attributes are serializable (no closures or non-string keys).Middleware Field Matching:
*) in middleware.fields match all fields, but except takes precedence. Test edge cases like nested arrays or objects.Inspect Results:
dd(Blasp::check($text)->toArray());
uniqueWords() and words() for granular details.Log Detected Words:
ProfanityDetected events:
Event::listen(ProfanityDetected::class, function ($event) {
Log::debug('Profanity detected:', $event->result->toArray());
});
Test Obfuscation:
$result = Blasp::driver('regex')->check('f-u-c-k');
$result->uniqueWords(); // Should return ['fuck']
Cache Issues:
Blasp::flushCache();
Custom Masking:
Blasp::mask(fn($word, $len) => str_repeat('X', $len))->check($text);
Dynamic Language Selection:
$language = request()->header('Accept-Language');
Blasp::in($language)->check($text);
Severity-Based Actions:
$score = Blasp::check($text)->score();
if ($score > 70) {
// Escalate to moderator
}
Batch Processing with Jobs:
SanitizeTextJob::dispatch($text)->onQueue('blasp');
Custom Dictionary:
php artisan vendor:publish --tag="blasp-languages"
resources/lang/blasp/english.php under words.How can I help you explore Laravel packages today?