expalmer / php-bad-words
PHP Bad words filtering. Checks for existence of bad word in a string but checks too an alone word or among the string
Installs: 30 668
Dependents: 3
Suggesters: 0
Security: 0
Stars: 14
Watchers: 2
Forks: 4
Open Issues: 1
Requires
- php: >=5.3.0
Requires (Dev)
This package is not auto-updated.
Last update: 2024-12-07 18:56:29 UTC
README
PHP BAD WORDS is a PHP Package that return TRUE or FALSE when it finds a bad word in a text.
The cool thing is that you can put rules in each word of the dictionary.
Explanation
- Alone: If the text is
You are a Asshole
, and the dictionary has the bad wordsarray( 'ass' );
it doesn't match, because the wordass
must be appears alone. - Among: The same text
You are a Asshole
and the same word, but now as an array with a rulearray( array('ass','among') );
, now it matches because the ruleamong
. It will find among each word of the text by the wordass
, and the wordasshole
has the wordass
, got it ;).
Installing
To install include it in your projects's composer.json
.
"expalmer/php-bad-words": "dev-master",
There are no additional dependencies required for this package to work.
Usage
/* Using Composer */ require_once 'vendor/autoload.php'; use \Expalmer\PhpBadWords\PhpBadWords as BadWords; $myDictionary = array( array("ass","among"), "anal", "butt" ); $myText = "You are an asshole"; $badwords = new BadWords(); $badwords->setDictionaryFromArray( $myDictionary ) ->setText( $myText ); var_dump( $badwords->check() ); // output: TRUE. // Because the dictionary has a word `ass` with `among` rule. var_dump( $badwords->checkAlone() ); // output: FALSE. // Because now it verified by word `ass` alone, and it does not exist alone. // ( It ignores dictionary word rules ) var_dump( $badwords->checkAmong() ); // output: TRUE. // Again, it verified by `ass` among each word in the text. // ( It ignores dictionary word rules ) // WITH THE WORD `anal` $myAnotherText = "She is not a research analyst"; $badwords->setText( myAnotherText ); var_dump( $badwords->check() ); // output: FALSE. var_dump( $badwords->checkAlone() ); // output: FALSE. var_dump( $badwords->checkAmong() ); // output: TRUE.
Setting the Dictionary
How the dictionary looks like.
array( array("ass","among"), // rule: among "anal", // rule: alone "beastiality", // rule: alone "fart", // rule: alone array("vag","among") // rule: among );
By an Array
$myDictionary = array( array("ass","among"), "butt" ); $bad = new PhpBadWords(); $obj->setDictionaryFromArray( $myDictionary );
By a File
/* // dictionary.php <?php return array( array("ass","among"), "anal", "butt" ); */ $bad = new PhpBadWords(); $obj->setDictionaryFromFile( __DIR__ . "/dictionary.php" );
Tests
To run the unit tests on this package, simply run vendor/bin/phpunit
from the package directory.