PHPackages                             elegantweb/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. elegantweb/sanitizer

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

elegantweb/sanitizer
====================

Sanitization library for PHP and the Laravel framework.

v2.4.0(3mo ago)1151.1M↓41.5%15[1 PRs](https://github.com/elegantweb/sanitizer/pulls)2MITPHPPHP ^8.0CI passing

Since Aug 11Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/elegantweb/sanitizer)[ Packagist](https://packagist.org/packages/elegantweb/sanitizer)[ RSS](/packages/elegantweb-sanitizer/feed)WikiDiscussions master Synced 4d ago

READMEChangelog (10)Dependencies (8)Versions (15)Used By (2)

sanitizer
=========

[](#sanitizer)

[![GitHub release (latest by date)](https://camo.githubusercontent.com/903a1c9cdfcf6f049eaa816a370c36602b13599f8d32dc230387802e4ec5c9a6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f656c6567616e747765622f73616e6974697a65723f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/903a1c9cdfcf6f049eaa816a370c36602b13599f8d32dc230387802e4ec5c9a6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f656c6567616e747765622f73616e6974697a65723f7374796c653d666c61742d737175617265)[![GitHub Workflow Status](https://camo.githubusercontent.com/76c9ddbf91bd1f607fba90152c97afef22662ca1e330875a774c6d71db129238/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f656c6567616e747765622f73616e6974697a65722f746573742e796d6c3f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/76c9ddbf91bd1f607fba90152c97afef22662ca1e330875a774c6d71db129238/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f656c6567616e747765622f73616e6974697a65722f746573742e796d6c3f7374796c653d666c61742d737175617265)

> Sanitization library for PHP and the Laravel framework.

Installation
------------

[](#installation)

```
composer require elegantweb/sanitizer
```

Usage
-----

[](#usage)

```
use Elegant\Sanitizer\Sanitizer;
use Elegant\Sanitizer\Filters\Enum;

$data = [
    'title' => ' ',
    'name' => ' sina ',
    'birth_date' => '06/25/1980',
    'email' => 'JOHn@DoE.com',
    'json' => '{"name":"value"}',
    'enum' => 'H',
];

$filters = [
    'title' => 'trim|empty_string_to_null',
    'name' => 'trim|empty_string_to_null|capitalize',
    'birth_date' => 'trim|empty_string_to_null|format_date:"m/d/Y","F j, Y"',
    'email' => ['trim', 'empty_string_to_null', 'lowercase'],
    'json' => 'cast:array',
    'enum' => ['trim', new Enum(BackedEnum::class)],
];

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

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

Will result in:

```
[
    'title' => null,
    'name' => 'Sina',
    'birth_date' => 'June 25, 1980',
    'email' => 'john@doe.com',
    'json' => ['name' => 'value'],
    'enum' => BackedEnum::Hearts,
];
```

### Laravel

[](#laravel)

In Laravel, you can use the Sanitizer through the Facade:

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

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 Elegant\Sanitizer\Laravel\SanitizesInput;

class MyAwesomeRequest extends Request
{
    use SanitizesInput;

    public function filters()
    {
        return [
            'name' => 'trim|capitalize',
        ];
    }
}
```

#### Optional

[](#optional)

If you are planning to use sanitizer for all of your HTTP requests, you can optionally disable Laravel's `TrimStrings` and `ConvertEmptyStringsToNull` middleware from your HTTP kernel.

```
protected $middleware = [
    [...]
    // \App\Http\Middleware\TrimStrings::class,
    // \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    [...]
];
```

Then, instead, you can use `trim` and `empty_string_to_null` filters:

```
$filters = [
    'some_string_parameter' => 'trim|empty_string_to_null',
];
```

Available Filters
-----------------

[](#available-filters)

The following filters are available out of the box:

FilterDescription**trim**Trims the given string**empty\_string\_to\_null**If the given string is empty set it to `null`**escape**Removes HTML tags and encodes special characters of the given string**lowercase**Converts the given string to all lowercase**uppercase**Converts the given string to all uppercase**capitalize**Capitalizes the given string**cast**Casts the given value into the given type. Options are: integer, float, string, boolean, object, array and Laravel Collection.**format\_date**Always takes two arguments, the given date's format and the target format, following DateTime notation.**strip\_tags**Strips HTML and PHP tags from the given string**digit**Removes all characters except digits from the given string**enum**Casts the given value to its corresponding enum typeCustom Filters
--------------

[](#custom-filters)

It is possible to use a closure or name of a class that implements `Elegant\Sanitizer\Contracts\Filter` interface.

```
class RemoveStringsFilter implements \Elegant\Sanitizer\Contracts\Filter
{
    public function apply($value, array $options = [])
    {
        return str_replace($options, '', $value);
    }
}

$filters = [
    'remove_strings' => RemoveStringsFilter::class,
    'password' => fn ($value, array $options = []) => sha1($value),
];

$sanitize = new Sanitizer($data, $filters);
```

### Laravel

[](#laravel-1)

You can 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, $closureOrClassName);
```

Inspiration
-----------

[](#inspiration)

- [WAAVI Sanitizer](https://github.com/Waavi/Sanitizer)

###  Health Score

61

—

FairBetter than 98% of packages

Maintenance82

Actively maintained with recent releases

Popularity55

Moderate usage in the ecosystem

Community23

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 86.8% 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 ~158 days

Recently: every ~282 days

Total

14

Last Release

97d ago

Major Versions

v1.1.0 → v2.0.02022-02-09

PHP version history (4 changes)v1.0.3PHP ^7.4

v1.0.4PHP ^7.4|^8.0

v2.0.0PHP ^8.0|^8.1

v2.1.0PHP ^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/10469774?v=4)[ElegantWeb](/maintainers/elegantweb)[@elegantweb](https://github.com/elegantweb)

---

Top Contributors

[![sharifzadesina](https://avatars.githubusercontent.com/u/205923701?v=4)](https://github.com/sharifzadesina "sharifzadesina (66 commits)")[![korridor](https://avatars.githubusercontent.com/u/26689068?v=4)](https://github.com/korridor "korridor (2 commits)")[![AlexJezior](https://avatars.githubusercontent.com/u/548744?v=4)](https://github.com/AlexJezior "AlexJezior (2 commits)")[![onlime](https://avatars.githubusercontent.com/u/2759561?v=4)](https://github.com/onlime "onlime (2 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (2 commits)")[![francoism90](https://avatars.githubusercontent.com/u/5028905?v=4)](https://github.com/francoism90 "francoism90 (1 commits)")[![u01jmg3](https://avatars.githubusercontent.com/u/1266205?v=4)](https://github.com/u01jmg3 "u01jmg3 (1 commits)")

---

Tags

laravellaravel-sanitizerphpsanitizationlaravelinputsanitationinput filterinput sanitationinput sanitizertransform input

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[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.0k4](/packages/sandermuller-laravel-fluent-validation)

PHPackages © 2026

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