joshuabedford / laravel-word-filter
A word and/or profanity filter for Laravel.
Installs: 66
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
Type:project
Requires
- php: >=5.5.9
Requires (Dev)
- laravel/laravel: 5.*
- orchestra/testbench: ~3.0
- phpunit/phpunit: ^7.5
- symfony/css-selector: 3.1.*
- symfony/dom-crawler: 3.1.*
README
P.S. Yes, I am cringing my way through this one. No, I don't know what most of these words/phrases mean.
Laravel Word Filter
A package that, once installed, allows prohibited words to be filtered out or refused. Works as standalone or with Laravel Validator. Utilizes two database tables to hold a whitelist and blacklist of acceptable and prohibited words. Can be used with Laravel's Form Validation functionality (E.G. prevent certain usernames) to refuse certain inputs and require user correction.
Installation
composer require joshuabedford/laravel-word-filter
Configure
php artisan vendor:publish
The command above will publish the appropriate files. Next, we have to install the database tables.
php artisan migrate
Now that the database tables are installed, we can seed them with data.
php artisan db:seed --class=JoshuaBedford\\LaravelWordFilter\\Database\\Seeds\\DatabaseSeeder
This should seed the database with our list of words that we consider necessary to blacklist or whitelist.
Usage
$string = app('wordFilter')->filter('something with a bad word');
The $string
will contain the filtered result.
You can also define things inline
$string = app('wordFilter')->replaceWith('#')->replaceFullWords(false)->filter('something with a bad word'));
You can also use the word
filter with Laravels Validation feature:
$request->validate([ 'username' => 'required|word|unique:users|max:255', ]);
Options
-
filter($string = string, $details = boolean)
pass a string to be filtered.- Enable details to have an array of results returned:
[ "orig" => "", "clean" => "", "hasMatch" => boolean, "matched" => [] ]
- Enable details to have an array of results returned:
-
reset()
resetreplaceWith
andreplaceFullWords
to defaults. -
replaceWith(string)
change the chars used to replace filtered strings. -
replaceFullWords(boolean)
enable to replace full words, disable to replace partial.
Credits
This package is based on LaravelProfanityFilter which is based on banbuilder.