askedio / laravel5-profanity-filter
A Vendor Package Example
Installs: 312 485
Dependents: 0
Suggesters: 0
Security: 0
Stars: 50
Watchers: 2
Forks: 16
Open Issues: 4
Requires
- php: >=5.5.9
Requires (Dev)
- laravel/laravel: 5.*
- phpunit/phpunit: ~5.0
- symfony/css-selector: 3.1.*
- symfony/dom-crawler: 3.1.*
This package is auto-updated.
Last update: 2024-11-29 04:52:20 UTC
README
Laravel Profanity Filter
Filter profanity, or other bad words, out of a string using Laravels localization feature or with any PHP application and some custom coding.
Installation
composer require askedio/laravel5-profanity-filter
Register in config/app.php.
Register the service providers in Laravel 5.4 or lower to enable the package:
Askedio\Laravel5ProfanityFilter\Providers\ProfanityFilterServiceProvider::class,
Configure
php artisan vendor:publish
You can edit the default list of words to filter along with the settings in config/profanity.php
.
replaceWith
can also be a string of chars to be randomly chosen to replace with, like '&%^@#'
.
You can create your own list of words, per language, in resources/lang/[language]/profanity.php
.
Usage
$string = app('profanityFilter')->filter('something with a bad word');
The $string
will contain the filtered result.
You can also define things inline
$string = app('profanityFilter')->replaceWith('#')->replaceFullWords(false)->filter('something with a bad word'));
You can also use the profanity
filter with Laravels Validation feature:
$request->validate([ 'title' => 'required|profanity|unique:posts|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.
Profanity Filter with PHP
You can also use this package without Laravel.
use Askedio\Laravel5ProfanityFilter\ProfanityFilter; $config = []; // Data from `resources/config/profanity.php` $badWordsArray = []; // Data from `resources/lang/[lang]/profanity.php` $profanityFilter = new ProfanityFilter($config, $badWordsArray); $string = $profanityFilter->filter('something with a bad word');
Credits
This package is based on banbuilder.