PHPackages                             orionstar/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. orionstar/bad-word-filter

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

orionstar/bad-word-filter
=========================

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

3.1.0(3y ago)066↓100%MITPHPPHP &gt;=7.4

Since Feb 24Pushed 3y agoCompare

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

READMEChangelogDependencies (1)Versions (11)Used By (0)

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

[](#badwordfilter)

A bad word filter for dependency free 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:

```
"orionstar/bad-word-filter": "3.*"

```

2. Run composer install

```
composer install
```

3. Add BadWordFilter to your use list

```
use orionstar\BadWordFilter\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 3 options:

```
$options = [
    'source'        => 'file',
    'source_file'   => __DIR__ . '/bad_words.php',
    'also_check'    => [],
];
```

###### 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.

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

###### 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 = ['also_check' => ['foobar']];
$filter = new \orionstar\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 = ['also_check' => ['cheesy']];
$cleanString = BadWordFilter::clean("my cheesy string", '#!%^", $myOptions);
var_dump($cleanString);
// output: "my #!%^ string"
```

or

```
$myOptions = ['also_check' => ['cheesy']];
$filter = new \orionstar\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 = ['also_check' => ['cheesy']];
$filter = new \orionstar\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 \orionstar\BadWordFilter\BadWordFilter();

if ($filter->isDirty(['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 \orionstar\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 \orionstar\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 = [
    'first' => [
        'bad' => [
            'a' => 'This is a bad string!',
            'b' => 'This is a good string!',
        ],
    ],
    'second' => 'bad bad bad string!',
];

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

if($badKeys = $filter->getDirtyKeysFromArray($arrayToCheck))
{
    var_dump($badKeys);
    /* output:

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

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity69

Established project with proven stability

 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 ~341 days

Recently: every ~627 days

Total

9

Last Release

1362d ago

Major Versions

v2.2.0 → 3.0.02022-05-08

PHP version history (2 changes)v2.0.0PHP &gt;=4.0.0

3.0.0PHP &gt;=7.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/cfc215984e2bb73bf876ab5f55510310a5c7d6cea02a63d6baf9ceec61e2e93d?d=identicon)[orionstar](/maintainers/orionstar)

---

Top Contributors

[![jcrowe206](https://avatars.githubusercontent.com/u/928788?v=4)](https://github.com/jcrowe206 "jcrowe206 (27 commits)")[![orionstar](https://avatars.githubusercontent.com/u/2486741?v=4)](https://github.com/orionstar "orionstar (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/orionstar-bad-word-filter/health.svg)

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

###  Alternatives

[funiq/geophp

Open-source native PHP library for doing geometry operations. Can read and write a wide variety of formats: (E)WKT, (E)WKB, TWKB, GeoJSON, KML, GPX, GeoRSS. Works with all Simple-Feature geometries (Point, LineString, Polygon...) and can be used to get centroids, bounding-boxes, area, etc.

21114.4k1](/packages/funiq-geophp)[moell/rss

moell/rss is a package that follows the RSS 2.0 standard

111.8k1](/packages/moell-rss)

PHPackages © 2026

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