blaspsoft/blasp
Advanced profanity filtering for Laravel with driver-based detection (regex/pattern/phonetic/pipeline), multi-language support, severity scoring (0–100), masking strategies, validation rules, middleware, Eloquent model integration, events, and test fakes.
composer require blaspsoft/blasp
php artisan vendor:publish --tag="blasp"
use Blaspsoft\Blasp\Facades\Blasp;
$result = Blasp::check("This is a test sentence with profanity");
if ($result->isOffensive()) {
echo $result->clean(); // "This is a test sentence with ********"
}
config/blasp.php to set default language, driver, and masking strategy.Blaspsoft\Blasp\Facades\Blasp for fluent API usage.config/blasp.php for global settings.Blaspsoft\Blasp\Rules\Profanity for form validation.Sanitize User Comments:
use Blaspsoft\Blasp\Facades\Blasp;
$comment = $request->input('comment');
$cleanedComment = Blasp::check($comment)->clean();
Fluent API Chaining:
$result = Blasp::in('spanish')
->driver('regex')
->mask('#')
->withSeverity(Severity::High)
->check($text);
Eloquent Integration:
class Comment extends Model {
use Blaspable;
protected $blaspable = ['body', 'title'];
}
Middleware for Request Filtering:
Route::post('/submit', SubmitController::class)
->middleware('blasp:sanitize,moderate');
Validation Rules:
use Blaspsoft\Blasp\Rules\Profanity;
$request->validate([
'bio' => ['required', Profanity::in('english')->maxScore(30)],
]);
Batch Processing:
$results = Blasp::checkMany([
"First text",
"Second text with profanity",
]);
Custom Drivers:
Extend Blaspsoft\Blasp\Contracts\Driver for domain-specific filtering.
Blade Directives:
<p>@clean($user->bio)</p>
Dynamic Language Selection:
$language = $user->preferredLanguage;
Blasp::in($language)->check($text);
Severity-Based Actions:
$result = Blasp::withSeverity(Severity::High)->check($text);
if ($result->severity() === Severity::High) {
// Escalate to moderator
}
Pipeline for Comprehensive Checks:
Blasp::pipeline('regex', 'phonetic', 'pattern')->check($text);
Performance with Regex Driver:
regex driver is the most accurate but slowest. Use pattern for speed or pipeline with regex only when needed.Blasp::cacheResults(true)->check($text);
False Positives in Phonetic Driver:
false_positives in config/blasp.php to avoid flagging common words like "fork" or "duck".max_distance_ratio if too many false positives/negatives occur.Masking Edge Cases:
Eloquent Trait Conflicts:
$blaspable attributes are not mass-assignable if using reject mode, or handle exceptions gracefully.Middleware Field Selection:
*) in middleware.fields checks all fields. Explicitly list fields for granular control:
'fields' => ['comment', 'title', 'bio'],
Inspect Results:
$result = Blasp::check($text);
dd($result->words()); // Detailed matched words with positions/severity
Test with Fake:
Blasp::fake([
'fuck' => ['severity' => Severity::High],
]);
Log Detected Profanity:
Blasp::check($text)->words()->each(function ($word) {
Log::warning("Profanity detected: {$word->word} (severity: {$word->severity})");
});
Language-Specific Settings:
phonetic) only support specific languages. Check drivers.phonetic.supported_languages.Cache Behavior:
cache.results to cache check results by content hash, but disable if content is highly dynamic.Severity Enums:
Severity enum matches the package's (Blaspsoft\Blasp\Enums\Severity).Custom Drivers:
class MyCustomDriver implements Driver {
public function check(string $text): Result {
// Implement custom logic
}
}
Register in config/blasp.php:
'drivers' => [
'my_custom' => MyCustomDriver::class,
],
Override Default Masking:
Blasp::mask(fn($word, $len) => str_repeat('X', $len))->check($text);
Extend Validation Rules:
use Blaspsoft\Blasp\Rules\Profanity as BaseProfanity;
class CustomProfanity extends BaseProfanity {
public function passes($attribute, $value) {
// Custom logic
}
}
Listen to Events:
Event::listen(ProfanityDetected::class, function ($event) {
// Handle detected profanity
});
'events' => env('APP_ENV') !== 'production',
pattern Driver for Bulk Checks:
Blasp::driver('pattern')->checkMany($largeArrayOfTexts);
withoutBlaspChecking for bulk operations where performance is critical.How can I help you explore Laravel packages today?