Giter Club home page Giter Club logo

badwordfilter's Introduction

BadWordFilter

Build Status Coverage Status Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

A bad word filter for php. Pass in a string or multidimensional array to check for the existence of a predefined list of bad words. Use the list that ships with the application or define your own custom blacklist. BadWordFilter only matches whole words (excluding symbols) and not partial words. This will match:

$myString = "Don't be a #FOOBAR!";
$clean = BadWordFilter::clean($myString);
var_dump($clean);
// output: "Don't be a #F****R!"

but this will not:

$myString = "I am an ASSociative professor";
$clean = BadWordFilter::clean($myString);
var_dump($clean);
// output: "I am an ASSociative professor"

QuickStart Guide

  1. add the following to your composer.json file:
"jcrowe/bad-word-filter": "2.2.*"
  1. Run composer install
composer install
  1. Add BadWordFilter to your providers array and create an alias to the facade in app.php
$providers = array(
   ...
   ...
   'JCrowe\BadWordFilter\Providers\BadWordFilterServiceProvider',
),

$aliases = array(
    ...
    ...
    'BadWordFilter'	  => 'JCrowe\BadWordFilter\Facades\BadWordFilter',
),
  1. start cleaning your inputs~
$cleanString = BadWordFilter::clean("my cheesy string");
var_dump($cleanString);
// output: "my c****y string"
INPORTANT NOTE
BadWordFilter does not and never will prevent XSS or SQL Injection. Take the proper steps in your code to sanitize all user input before storing to a database or displaying to the client.

Settings options

BadWordFilter takes 4 options:

$options = array(
    'source' => 'file',
    'source_file' => __DIR__ . '/bad_words.php',
    'strictness' => 'very_strict',
    'also_check' => array(),
);
Source Types

File

If you specify a source type of "file" you must also specify a source_file or use the default source file included with this package. The Source File must return an array of words to check for. If you wish to specify strictness level in your custom bad words list simply split your array into sub keys of 'permissive', 'lenient', 'strict', 'very_strict', 'strictest', 'misspellings'

Array

If you specify a source type of "array" you must also specify a "bad_words_array" key that contains a list of words to check for.

Strictness

Available options are: "permissive", "lenient", "strict", "very_strict", "strictest", "misspellings"

Where permissive will allow all but the worst of words through and strictest will attempt to flag even the most G rated words. Mispellings will also check for common misspellings and/or leet-speak. A full list of words can be seen in the src/config/bad_words.php file in this repo.

Also Check

In addition to the default list specified in the config file or array you can also pass in an "also_check" key that contains an array of words to flag.

Overriding Defaults

You can override the default settings in the constructor if using the class as an instance, or as an optional parameter in the static method call

$myOptions = array('strictness' => 'permissive', 'also_check' => array('foobar'));
$filter = new \JCrowe\BadWordFilter\BadWordFilter($myOptions);

$cleanString = $filter->clean('Why did you FooBar my application?');
var_dump($cleanString);
// output: "Why did you F****r my application?"

How to handle bad words

By default bad words will be replaced with the first letter followed by the requisite number of asterisks and then the last letter. Ie: "Cheese" would become "C****e"

This can be changed to be replaced with a set string by passing the new string as an argument to the "clean" method

$myOptions = array('also_check' => array('cheesy'));
$cleanString = BadWordFilter::clean("my cheesy string", '#!%^", $myOptions);
var_dump($cleanString);
// output: "my #!%^ string"

or

$myOptions = array('also_check' => array('cheesy'));
$filter = new \JCrowe\BadWordFilter\BadWordFilter($myOptions);
$cleanString = $filter->clean("my cheesy string", "#!$%");
var_dump($cleanString);
// output: "my #!$% string"

In case you want to keep bad word and surround it by anything (ex. html tag):

$myOptions = array('also_check' => array('cheesy'));
$filter = new \JCrowe\BadWordFilter\BadWordFilter($myOptions);
$cleanString = $filter->clean("my cheesy string", '<span style="color: red;">$0</span>');
var_dump($cleanString);
// output: "my <span style="color: red;">cheesy</span> string"

Full method list

isDirty
Check if a string or an array contains a bad word

Params: $input - required - array|string

Return: Boolean

Usage:

$filter = new \JCrowe\BadWordFilter\BadWordFilter();

if ($filter->isDirty(array('this is a dirty string')) {
    /// do something
}
clean
Clean bad words from a string or an array. By default bad words are replaced with asterisks with the exception of the first and last letter. Optionally you can specify a string to replace the words with

Params: $input - required - array|string $replaceWith - optional - string

Return: Cleaned array or string

Usage:

$filter = new \JCrowe\BadWordFilter\BadWordFilter();
$string = "this really bad string";
$cleanString = $filter->clean($string);
STATIC clean
Static wrapper around the "clean" method.

Params: $input - required - array|string $replaceWith - optional - string $options - optional - array

Return: Cleaned array or string

Usage:

$string = "this really bad string";
$cleanString = BadWordFilter::clean($string);
getDirtyWordsFromString
Return the matched dirty words

Params: $input - required - string

Return: Boolean

Usage:

$filter = new \JCrowe\BadWordFilter\BadWordFilter();
if ($badWords = $filter->getDirtyWordsFromString("this really bad string")) {
    echo "You said these bad words: " . implode("<br />", $badWords);
}
getDirtyKeysFromArray
After checking an array using the isDirty method you can access the bad keys by using this method

Params : none

Return: String - dot notation of array keys

Usage:

$arrayToCheck = array(
    'first' => array(
        'bad' => array(
            'a' => 'This is a bad string!',
            'b' => 'This is a good string!',
        ),
    ),
    'second' => 'bad bad bad string!',
);

$filter = new \JCrowe\BadWordFilter\BadWordFilter();

if ($badKeys = $filter->getDirtyKeysFromArray($arrayToCheck)) {

    var_dump($badKeys);
    /* output:

        array(
            0 => 'first.bad.a',
            1 => 'second'
        );
    */
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

badwordfilter's People

Contributors

jcrowe206 avatar dependabot[bot] avatar github-actions[bot] avatar thamaraiselva avatar palpalani avatar tpv-ebben avatar vadimdez avatar

Stargazers

 avatar

Watchers

James Cloos avatar

Forkers

palpalani

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.