Skip to content

Badwords PHP is a small lightweight PHP library for detecting badwords, e.g. profanity, in content.

License

Notifications You must be signed in to change notification settings

cledilsonweb/badwords

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Badwords PHP

Badwords PHP is small lightweight PHP library for detecting "bad" words, e.g. profanity, in content. This is a fork from mioga-technik/badwords to customize.

Aside from the obvious matching if a word is present in a string, the filter also tries to detect words similar to those in the list, e.g. gl@d and glad.

The library is designed to be highly configurable, from the word lists used to the character replacement configuration at the heart of the filter.

Note: At present the default configuration provided is not a bulletproof/catch-all solution, but it will catch most variations. This will become more robust over time.

Requirements

  • The library is only supported on PHP 5.6 and up.
  • Composer is required.

Installation

To install include it in your projects's composer.json.

    $ composer require cledilsonweb/badwords
    $ composer update

There are no additional dependencies required for this package to work.

Usage

File Method:

The simplest way to use the library is as follows,

    $dictionary = new \Badword\Dictionary\Php('path/to/dictionary_list.php');
    $config = new \Badword\Filter\Config\Standard();
    $filter = new \Badword\Filter($dictionary, $config);

    $result = $filter->filter('My content...');
    $result->getRiskLevel();
    $result->getMatches();
    $result->getMatchesAndRiskLevels();
    $result->getHighlightedContent();

Explained,

  • First load your list of "bad" words using the Dictionary objects, or create your own and implement the Dictionary interface.
  • Define a configuration for the filter to use (a default Standard configuration is provided).
  • Create the Filter passing your dictionary(s) and config.
  • Filter your content using the filter() method.
  • Use the Result object to analyse your content.

Array Method:

    // An example moderate dictionary.
    $dictionaryWords = array(
        'some',
        'bad',
        'words'
    );

    $dictionary = new \Badword\Dictionary\PhpArray($dictionaryWords, 1);
    $config = new \Badword\Filter\Config\Standard();
    $filter = new \Badword\Filter($dictionary, $config);

    $result = $filter->filter('My content...');
    $result->getRiskLevel();
    $result->getMatches();
    $result->getMatchesAndRiskLevels();
    $result->getHighlightedContent();

Object Layer Method [new]:

   $dictionaryWords = array(
        'reject' => array(
            'maecenas',
            'mauris',
            'luctus'
        ),
        'moderate' => array(
            'consectetur',
            'neque',
            'velit'
        )
   );
    
   $Badwords = new \Badword\Badwords($dictionaryWords);
    
   $result = $Badwords->Filter()->filter('My content...'));
   $result->getRiskLevel();
   $result->getMatches();
   $result->getMatchesAndRiskLevels();
   $result->getHighlightedContent();

Testing

To run the unit tests on this package, simply run vendor/bin/phpunit from the package directory.

Credits

Previous authors:

About

Badwords PHP is a small lightweight PHP library for detecting badwords, e.g. profanity, in content.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 98.3%
  • CSS 1.7%