This little package tests a string against a disallowlist.
If you are looking for a Laravel-specific implementation, see https://github.com/accentinteractive/laravel-disallowlister
The isDisallowed() method can use wildcards, like *.
Under the hood, accentinteractive/disallowtester uses fnmatch(), so you can use the same wildcards as in that php function (the globbing wildcard patterns):
-
*sex*disallows sex, sexuality and bisexual. -
cycle*disallows cycle and cycles, but not bicycle. -
m[o,u]mdisallows mom and mum, but allows mam. -
m?ndisallows man and men, but allows moon.
You can install the package via composer:
composer require accentinteractive/disallowlisterYou can pass the disallowlist in the constructor or via other methods.
// Pass the disallowlist in the constructor
$disallowLister = new DisallowLister(['foo']); // ['foo']
// Set the disallowlist in the setter method
$disallowLister->setDisallowList(['bar']); // ['bar']
// Add an item to the disallowlist
$disallowLister->add('baz'); // ['bar', 'baz']
// Add multiple items to the disallowlist
$disallowLister->add(['bat', 'fiz']); // ['bar', 'baz', 'bat', 'fiz']
// Remove an item from the disallowlist
$disallowLister->remove('fiz'); // ['bar', 'baz', 'bat']
// Remove multiple items from the disallowlist
$disallowLister->remove(['baz', 'bat']); // ['bar']## Literal string
$disallowLister = new DisallowLister(['bar', 'foo']);
$disallowLister->isDisallowed('bar'); // Returns true
$disallowLister->isDisallowed('bars'); // Returns false
## Wildcards
// Under the hood, `accentinteractive/disallowtester`
// uses `fnmatch()`, so you can use the same
// wildcards as in that php function (the
// globbing wildcard patterns):
(new DisallowLister(['b?r']))->isDisallowed('bar'); // Returns true
(new DisallowLister(['m[o,u]m']))->isDisallowed('mom'); // Returns true
(new DisallowLister(['*bar*']))->isDisallowed('I like crowbars'); // Returns true// By default, matching is not case sensitive
(new DisallowLister(['bar']))->isDisallowed('BAR'); // Returns true
// To set case sensitive matching
(new DisallowLister(['bar']))->caseSensitive(true)->isDisallowed('BAR'); // Returns false// By default the entire string is checked.
(new DisallowLister())->setDisallowList(['bar'])->isDisallowed('My favorite bar'); // Returns false
// Check word for word.
(new DisallowLister())->setDisallowList(['bar'])->setWordForWord(true)->isDisallowed('My favorite bar'); // Returns truecomposer testPlease see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email joost@accentinteractive.nl instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.