PHPackages                             binary-cats/sanitizer - 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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. binary-cats/sanitizer

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

binary-cats/sanitizer
=====================

Data sanitizer and Laravel form requests with input sanitation.

13.0.0(3mo ago)5104.0k↓44.3%4MITPHPCI failing

Since Sep 25Pushed 3mo agoCompare

[ Source](https://github.com/binary-cats/sanitizer)[ Packagist](https://packagist.org/packages/binary-cats/sanitizer)[ RSS](/packages/binary-cats-sanitizer/feed)WikiDiscussions v13.x Synced 3d ago

READMEChangelog (6)Dependencies (8)Versions (28)Used By (0)

[![](https://camo.githubusercontent.com/2bedf63f24cda7efab02da955dc11fb7ef8a060e2f26b73c33a7aac84529b8a3/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f737570706f72742d756b7261696e652e7376673f743d31)](https://supportukrainenow.org)

Sanitizer
=========

[](#sanitizer)

[![Latest Version on Packagist](https://camo.githubusercontent.com/96355e572e5669f56ae0924d873a63f646e37cf01cd1c664d8e7acbcc957329a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f62696e6172792d636174732f73616e6974697a65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/binary-cats/sanitizer)[![Tests](https://camo.githubusercontent.com/02785055fa72f42a5c215d4aff1de3eaa0e803eef26aa85c2a6a8c1c5ddaee7d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f62696e6172792d636174732f73616e6974697a65722f72756e2d74657374732e796d6c3f6272616e63683d7631332e78266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/binary-cats/sanitizer/actions/workflows/run-tests.yml)[![Code Style Action Status](https://camo.githubusercontent.com/61982c51f6fca7f7f33a40fccc2fdc970d93cabd95374aabc82a7372727a2808/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3331363736333635332f736869656c643f6272616e63683d7631332e78)](https://github.styleci.io/repos/316763653)

Introduction
------------

[](#introduction)

Sanitizer provides your Laravel application with an easy way to format user input, both through the provided filters or through custom ones that can easily be added to the Sanitizer library.

[![](https://repository-images.githubusercontent.com/316763653/71c29400-3164-11eb-8a35-d4b16ac34253)](https://repository-images.githubusercontent.com/316763653/71c29400-3164-11eb-8a35-d4b16ac34253)

Example
-------

[](#example)

Given a data array with the following format:

```
    $data = [
        'first_name'    =>  'john',
        'last_name'     =>  'DOE',
        'email'         =>  '  JOHn@DoE.com',
        'birthdate'     =>  '06/25/1980',
        'jsonVar'       =>  '{"name":"value"}',
        'description'   =>  'Test paragraph. Other text',
        'phone'         =>  '+08(096)90-123-45q',
        'country'       =>  'GB',
        'postcode'      =>  'ab12 3de',
    ];
```

We can easily format it using our Sanitizer and the some of Sanitizer's default filters:

```
    use BinaryCats\Sanitizer\Sanitizer;

    $filters = [
        'first_name'    =>  'trim|escape|capitalize',
        'last_name'     =>  'trim|escape|capitalize',
        'email'         =>  'trim|escape|lowercase',
        'birthdate'     =>  'trim|format_date:m/d/Y, Y-m-d',
        'jsonVar'       =>  'cast:array',
        'description'   =>  'strip_tags',
        'phone'         =>  'digit',
        'country'       =>  'trim|escape|capitalize',
        'postcode'      =>  'trim|escape|uppercase|filter_if:country,GB',
    ];

    $sanitizer  = new Sanitizer($data, $filters);

    var_dump($sanitizer->sanitize());
```

Which will yield:

```
    [
        'first_name'    =>  'John',
        'last_name'     =>  'Doe',
        'email'         =>  'john@doe.com',
        'birthdate'     =>  '1980-06-25',
        'jsonVar'       =>  '["name" => "value"]',
        'description'   =>  'Test paragraph. Other text',
        'phone'         =>  '080969012345',
        'country'       =>  'GB',
        'postcode'      =>  'AB12 3DE',
    ];
```

It's usage is very similar to Laravel's Validator module, for those who are already familiar with it, although Laravel is not required to use this library.

Filters are applied in the same order they're defined in the $filters array. For each attribute, filters are separered by | and options are specified by suffixing a comma separated list of arguments (see format\_date).

Available filters
-----------------

[](#available-filters)

The following filters are available out of the box:

FilterDescription**trim**Trims a string**escape**Escapes HTML and special chars using php's filter\_var**lowercase**Converts the given string to all lowercase**uppercase**Converts the given string to all uppercase**capitalize**Capitalize a string**cast**Casts a variable into the given type. Options are: integer, float, string, boolean, object, array and Laravel Collection.**format\_date**Always takes two arguments, the date's given format and the target format, following DateTime notation.**strip\_tags**Strip HTML and PHP tags using php's strip\_tags**digit**Get only digit characters from the string**filter\_if**Applies filters if an attribute exactly matches valueAdding custom filters
---------------------

[](#adding-custom-filters)

You can add your own filters by passing a custom filter array to the Sanitize constructor as the third parameter. For each filter name, either a closure or a full classpath to a Class implementing the `BinaryCats\Sanitizer\Contracts\Filter` interface must be provided.

```
use BinaryCats\Sanitizer\Contracts\Filter;

class RemoveStringsFilter implements Filter
{
    /**
     * Apply filter
     *
     * @param  mixed $value
     * @param  array  $options
     * @return string
     */
    public function apply($value, $options = [])
    {
        return str_replace($options, '', $value);
    }
}
```

Closures must always accept two parameters: $value and an $options array:

```
$customFilters = [
    'hash'   =>  function($value, $options = []) {
            return sha1($value);
        },
    'remove_strings' => RemoveStringsFilter::class,
];

$filters = [
    'secret'    =>  'hash',
    'text'      =>  'remove_strings:Curse,Words,Galore',
];

$sanitize = new Sanitize($data, $filters, $customFilters);
```

Install
-------

[](#install)

To install, just run:

```
composer require binary-cats/sanitizer

```

And you're done! If you're using Laravel, your application will autoamtically register Service provider, as well as the Sanitizer Facade:

If you prefer to do that manually, you need to add the values to your `config/app.php`:

```
    'providers' => [
        ...
        BinaryCats\Sanitizer\Laravel\SanitizerServiceProvider::class,
    ];

    'aliases' => [
        ...
        'Sanitizer' => BinaryCats\Sanitizer\Laravel\Facade::class,
    ];
```

Laravel goodies
---------------

[](#laravel-goodies)

If you are using Laravel, you can use the Sanitizer through the Facade:

```
    $newData = \Sanitizer::make($data, $filters)->sanitize();
```

You can also easily extend the Sanitizer library by adding your own custom filters, just like you would the Validator library in Laravel, by calling extend from a ServiceProvider like so:

```
    \Sanitizer::extend($filterName, $closureOrClassPath);
```

You may also Sanitize input in your own FormRequests by using the SanitizesInput trait, and adding a *filters* method that returns the filters that you want applied to the input.

```
    namespace App\Http\Requests;

    use App\Http\Requests\Request;
    use BinaryCats\Sanitizer\Laravel\SanitizesInput;

    class SanitizedRequest extends Request
    {
        use SanitizesInput;

        public function filters()
        {
            return [
                'name'  => 'trim|capitalize',
                'email' => 'trim',
                'text'  => 'remove_strings:Curse,Words,Galore',
            ];
        }

        public function customFilters()
        {
            return [
                'remove_strings' => RemoveStringsFilter::class,
            ];
        }

        /* ... */
```

To generate a Sanitized Request just execute the included Artisan command:

```
php artisan make:sanitized-request TestSanitizedRequest

```

The only difference with a Laravel FormRequest is that now you'll have an extra 'fields' method in which to enter the input filters you wish to apply, and that input will be sanitized before being validated.

Support us
----------

[](#support-us)

[Binary Cats](https://binarycats.dev) is a webdesign agency based in Illinois, US.

### License

[](#license)

Sanitizer is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)

Thanks
------

[](#thanks)

Many than for the original [WAAVI Sanitizer](https://github.com/Waavi/Sanitizer) which is the source for this repo. Unfortunately it does not appear to be maintained anymore.

###  Health Score

56

—

FairBetter than 97% of packages

Maintenance80

Actively maintained with recent releases

Popularity37

Limited adoption so far

Community20

Small or concentrated contributor base

Maturity73

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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

Recently: every ~153 days

Total

23

Last Release

106d ago

Major Versions

1.0.16 → 8.0.02020-11-28

8.0.0 → 9.0.02022-01-18

9.2 → 12.0.02024-07-17

9.x-dev → v12.0.x-dev2025-04-02

v12.0.x-dev → 13.0.02026-03-20

### Community

Maintainers

![](https://www.gravatar.com/avatar/ebb48367388b4368b14cca42714bb13002d7414d9dc8da19c5490ef65c059719?d=identicon)[cyrill.kalita@gmail.com](/maintainers/cyrill.kalita@gmail.com)

---

Top Contributors

[![cyrillkalita](https://avatars.githubusercontent.com/u/2401848?v=4)](https://github.com/cyrillkalita "cyrillkalita (31 commits)")[![sildraug](https://avatars.githubusercontent.com/u/767887?v=4)](https://github.com/sildraug "sildraug (9 commits)")[![zarianec](https://avatars.githubusercontent.com/u/3776001?v=4)](https://github.com/zarianec "zarianec (3 commits)")[![StyleCIBot](https://avatars.githubusercontent.com/u/11048387?v=4)](https://github.com/StyleCIBot "StyleCIBot (3 commits)")[![alariva](https://avatars.githubusercontent.com/u/3021314?v=4)](https://github.com/alariva "alariva (2 commits)")[![shekharkhatri](https://avatars.githubusercontent.com/u/44218196?v=4)](https://github.com/shekharkhatri "shekharkhatri (2 commits)")[![TiagoSilvaPereira](https://avatars.githubusercontent.com/u/11933789?v=4)](https://github.com/TiagoSilvaPereira "TiagoSilvaPereira (2 commits)")[![LasseRafn](https://avatars.githubusercontent.com/u/2689341?v=4)](https://github.com/LasseRafn "LasseRafn (1 commits)")[![mozammil](https://avatars.githubusercontent.com/u/1447522?v=4)](https://github.com/mozammil "mozammil (1 commits)")[![naabster](https://avatars.githubusercontent.com/u/1961461?v=4)](https://github.com/naabster "naabster (1 commits)")[![nickfls](https://avatars.githubusercontent.com/u/12013206?v=4)](https://github.com/nickfls "nickfls (1 commits)")[![norbybaru](https://avatars.githubusercontent.com/u/13020317?v=4)](https://github.com/norbybaru "norbybaru (1 commits)")[![sharifzadesina](https://avatars.githubusercontent.com/u/205923701?v=4)](https://github.com/sharifzadesina "sharifzadesina (1 commits)")[![Tlapi](https://avatars.githubusercontent.com/u/2815391?v=4)](https://github.com/Tlapi "Tlapi (1 commits)")[![francoism90](https://avatars.githubusercontent.com/u/5028905?v=4)](https://github.com/francoism90 "francoism90 (1 commits)")[![smknstd](https://avatars.githubusercontent.com/u/2412608?v=4)](https://github.com/smknstd "smknstd (1 commits)")[![Spodnet](https://avatars.githubusercontent.com/u/3228770?v=4)](https://github.com/Spodnet "Spodnet (1 commits)")[![fomvasss](https://avatars.githubusercontent.com/u/19834478?v=4)](https://github.com/fomvasss "fomvasss (1 commits)")[![Fleeym](https://avatars.githubusercontent.com/u/61891787?v=4)](https://github.com/Fleeym "Fleeym (1 commits)")[![grpaiva](https://avatars.githubusercontent.com/u/4790659?v=4)](https://github.com/grpaiva "grpaiva (1 commits)")

---

Tags

input-filterinput-sanitizationlaravelsanitizerlaravelinputsanitationinput filterinput sanitationinput sanitizertransform inputbinary-cats

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/binary-cats-sanitizer/health.svg)

```
[![Health](https://phpackages.com/badges/binary-cats-sanitizer/health.svg)](https://phpackages.com/packages/binary-cats-sanitizer)
```

###  Alternatives

[elegantweb/sanitizer

Sanitization library for PHP and the Laravel framework.

1151.1M3](/packages/elegantweb-sanitizer)[waavi/sanitizer

Data sanitizer and Laravel 7 form requests with input sanitation.

433589.9k5](/packages/waavi-sanitizer)[laravel/horizon

Dashboard and code-driven configuration for Laravel queues.

4.2k95.4M307](/packages/laravel-horizon)[sandermuller/laravel-fluent-validation

Fluent validation rule builders for Laravel

20719.1k4](/packages/sandermuller-laravel-fluent-validation)

PHPackages © 2026

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