PHPackages                             satnin/bad-word-filter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. satnin/bad-word-filter

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

satnin/bad-word-filter
======================

PHP Bad word filtering. Checks for the existence of a bad word in a string or array.

v1.0.3(1y ago)0164MITPHPPHP 8.\*

Since Apr 6Pushed 1y agoCompare

[ Source](https://github.com/satnin/BadWordFilter)[ Packagist](https://packagist.org/packages/satnin/bad-word-filter)[ RSS](/packages/satnin-bad-word-filter/feed)WikiDiscussions master Synced today

READMEChangelog (3)Dependencies (4)Versions (5)Used By (0)

BadWordFilter
=============

[](#badwordfilter)

[![Build Status](https://camo.githubusercontent.com/8b6b8c83be8d601f1f49ead5b4ee27039aa4084a337548a270b90e69da430dc6/68747470733a2f2f7472617669732d63692e6f72672f6a63726f77653230362f426164576f726446696c7465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/jcrowe206/BadWordFilter) [![Coverage Status](https://camo.githubusercontent.com/34f95d1b2c1b6f2760cfc89f159f9d2b1bd5a13c9a92ac55e01045d7ee7b5367/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6a63726f77653230362f426164576f726446696c7465722f62616467652e7376673f6272616e63683d76322e312e3226736572766963653d676974687562)](https://coveralls.io/github/jcrowe206/BadWordFilter?branch=v2.1.2)

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

[](#quickstart-guide)

1. add the following to your composer.json file:

```
"jcrowe/bad-word-filter": "2.2.*"
```

2. Run composer install

```
composer install
```

3. 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',
),
```

4. start cleaning your inputs~

```
$cleanString = BadWordFilter::clean("my cheesy string");
var_dump($cleanString);
// output: "my c****y string"
```

##### INPORTANT NOTE

[](#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.**

[](#badwordfilter-does-not-and-never-will-prevent-xss-or-sql-injection-take-the-proper-steps-in-your-code-to-sanitize-all-user-input-beforestoring-to-a-database-or-displaying-to-the-client)

### Settings options

[](#settings-options)

BadWordFilter takes 4 options:

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

###### Source Types

[](#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

[](#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

[](#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

[](#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

[](#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", '$0');
var_dump($cleanString);
// output: "my cheesy string"
```

### Full method list

[](#full-method-list)

###### isDirty

[](#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)

 **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-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

[](#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("", $badWords);
}
```

###### getDirtyKeysFromArray

[](#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'
        );
    */
}
```

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 79.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~580 days

Total

4

Last Release

539d ago

PHP version history (2 changes)v1.0.0PHP &gt;=5.3.0

v1.0.1PHP 8.\*

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/110102854?v=4)[Saturnin Wanonkou](/maintainers/satnin)[@satnin](https://github.com/satnin)

---

Top Contributors

[![jcrowe206](https://avatars.githubusercontent.com/u/928788?v=4)](https://github.com/jcrowe206 "jcrowe206 (27 commits)")[![satnin](https://avatars.githubusercontent.com/u/110102854?v=4)](https://github.com/satnin "satnin (5 commits)")[![tpv-ebben](https://avatars.githubusercontent.com/u/28765222?v=4)](https://github.com/tpv-ebben "tpv-ebben (1 commits)")[![VadimDez](https://avatars.githubusercontent.com/u/3748453?v=4)](https://github.com/VadimDez "VadimDez (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/satnin-bad-word-filter/health.svg)

```
[![Health](https://phpackages.com/badges/satnin-bad-word-filter/health.svg)](https://phpackages.com/packages/satnin-bad-word-filter)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[renatomarinho/laravel-page-speed

Laravel Page Speed

2.5k1.7M11](/packages/renatomarinho-laravel-page-speed)[illuminate/pagination

The Illuminate Pagination package.

12234.1M1.0k](/packages/illuminate-pagination)[illuminate/pipeline

The Illuminate Pipeline package.

9349.2M282](/packages/illuminate-pipeline)[illuminate/redis

The Illuminate Redis package.

8314.6M375](/packages/illuminate-redis)[spatie/laravel-export

Create a static site bundle from a Laravel app

674146.0k6](/packages/spatie-laravel-export)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
