PHPackages                             calamarmihai/laravel-request-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. calamarmihai/laravel-request-sanitizer

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

calamarmihai/laravel-request-sanitizer
======================================

Data sanitizer and Laravel form requests with input sanitation.

9.x-dev(2y ago)01MITPHP

Since Aug 3Pushed 2y agoCompare

[ Source](https://github.com/CalamarMihai/laravel-sanitizer)[ Packagist](https://packagist.org/packages/calamarmihai/laravel-request-sanitizer)[ RSS](/packages/calamarmihai-laravel-request-sanitizer/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (2)Used By (0)

Sanitizer
=========

[](#sanitizer)

[![https://github.com/binary-cats/sanitizer/actions](https://github.com/binary-cats/sanitizer/workflows/Laravel/badge.svg)](https://github.com/binary-cats/sanitizer/workflows/Laravel/badge.svg)[![https://github.styleci.io/repos/316763653](https://camo.githubusercontent.com/4dfd13ecf226a3a28320914022c6f7d539d8c4aa18767be393357cefcbace50a/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3331363736333635332f736869656c64)](https://camo.githubusercontent.com/4dfd13ecf226a3a28320914022c6f7d539d8c4aa18767be393357cefcbace50a/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3331363736333635332f736869656c64)[![https://scrutinizer-ci.com/g/binary-cats/sanitizer/](https://camo.githubusercontent.com/52fded753da8d3281dd574ecaad6157f3350405423a3bd304138c8ef5c389744/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f62696e6172792d636174732f73616e6974697a65722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://camo.githubusercontent.com/52fded753da8d3281dd574ecaad6157f3350405423a3bd304138c8ef5c389744/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f62696e6172792d636174732f73616e6974697a65722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)[![Build Status](https://camo.githubusercontent.com/f6af39fc29924d9bdf400712b73ce992fd0c16c6892f13790b7f54757d7f3449/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f62696e6172792d636174732f73616e6974697a65722f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/binary-cats/sanitizer)

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 calamar-mihai\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 `calamar-mihai\Sanitizer\Contracts\Filter` interface must be provided.

```
use calamar-mihai\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' => [
        ...
        calamar-mihai\Sanitizer\Laravel\SanitizerServiceProvider::class,
    ];

    'aliases' => [
        ...
        'Sanitizer' => calamar-mihai\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 calamar-mihai\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.

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

17

—

LowBetter than 6% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity1

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity29

Early-stage or recently created project

 Bus Factor3

3 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

Unknown

Total

1

Last Release

1013d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6711033cf5f95a99e7b666a6c6d1e6c25c1895010b5f09202f56ff17cbed40c7?d=identicon)[CalamarMihai](/maintainers/CalamarMihai)

---

Top Contributors

[![cyrillkalita](https://avatars.githubusercontent.com/u/2401848?v=4)](https://github.com/cyrillkalita "cyrillkalita (13 commits)")[![sildraug](https://avatars.githubusercontent.com/u/767887?v=4)](https://github.com/sildraug "sildraug (9 commits)")[![mihaicalamar](https://avatars.githubusercontent.com/u/49311014?v=4)](https://github.com/mihaicalamar "mihaicalamar (5 commits)")[![zarianec](https://avatars.githubusercontent.com/u/3776001?v=4)](https://github.com/zarianec "zarianec (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)")[![grpaiva](https://avatars.githubusercontent.com/u/4790659?v=4)](https://github.com/grpaiva "grpaiva (1 commits)")[![mozammil](https://avatars.githubusercontent.com/u/1447522?v=4)](https://github.com/mozammil "mozammil (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)")[![StyleCIBot](https://avatars.githubusercontent.com/u/11048387?v=4)](https://github.com/StyleCIBot "StyleCIBot (1 commits)")[![fomvasss](https://avatars.githubusercontent.com/u/19834478?v=4)](https://github.com/fomvasss "fomvasss (1 commits)")[![jijoel](https://avatars.githubusercontent.com/u/3487641?v=4)](https://github.com/jijoel "jijoel (1 commits)")[![LasseRafn](https://avatars.githubusercontent.com/u/2689341?v=4)](https://github.com/LasseRafn "LasseRafn (1 commits)")

---

Tags

laravelinputsanitationinput filterinput sanitationinput sanitizertransform inputbws

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/calamarmihai-laravel-request-sanitizer/health.svg)

```
[![Health](https://phpackages.com/badges/calamarmihai-laravel-request-sanitizer/health.svg)](https://phpackages.com/packages/calamarmihai-laravel-request-sanitizer)
```

###  Alternatives

[elegantweb/sanitizer

Sanitization library for PHP and the Laravel framework.

115950.4k2](/packages/elegantweb-sanitizer)[waavi/sanitizer

Data sanitizer and Laravel 7 form requests with input sanitation.

433589.2k5](/packages/waavi-sanitizer)[propaganistas/laravel-phone

Adds phone number functionality to Laravel based on Google's libphonenumber API.

3.0k35.7M107](/packages/propaganistas-laravel-phone)[spatie/laravel-honeypot

Preventing spam submitted through forms

1.6k6.0M60](/packages/spatie-laravel-honeypot)[proengsoft/laravel-jsvalidation

Validate forms transparently with Javascript reusing your Laravel Validation Rules, Messages, and FormRequest

1.1k2.3M49](/packages/proengsoft-laravel-jsvalidation)[axlon/laravel-postal-code-validation

Worldwide postal code validation for Laravel and Lumen

3853.3M1](/packages/axlon-laravel-postal-code-validation)

PHPackages © 2026

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