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.3.1(1y ago)115950.4k—9.8%15[1 PRs](https://github.com/elegantweb/sanitizer/pulls)2MITPHPPHP ^8.0CI passing

Since Aug 11Pushed 1mo ago1 watchersCompare

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

READMEChangelog (10)Dependencies (4)Versions (14)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

57

—

FairBetter than 98% of packages

Maintenance70

Regular maintenance activity

Popularity55

Moderate usage in the ecosystem

Community22

Small or concentrated contributor base

Maturity67

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

Recently: every ~269 days

Total

13

Last Release

444d 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://www.gravatar.com/avatar/4331089ae6e2d73d6307de5a1ca720442ca9d45a762c418e5b45e8b108094d15?d=identicon)[elegantweb](/maintainers/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.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)[wendelladriel/laravel-validated-dto

Data Transfer Objects with validation for Laravel applications

759569.4k13](/packages/wendelladriel-laravel-validated-dto)

PHPackages © 2026

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