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

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

xiaoyiqingz/sanitizer
=====================

Sanitization library for PHP and the Laravel framework.

v0.0.1(10mo ago)054MITPHPPHP ^7.3|^8.0

Since Jul 11Pushed 10mo agoCompare

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

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

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/30bd0ab04d6e37a95b7ffe5fef528d322ce0ce1ac2f58b98c80b682c2467f1c6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f656c6567616e747765622f73616e6974697a65722f746573743f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/30bd0ab04d6e37a95b7ffe5fef528d322ce0ce1ac2f58b98c80b682c2467f1c6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f656c6567616e747765622f73616e6974697a65722f746573743f7374796c653d666c61742d737175617265)

> Sanitization library for PHP and the Laravel framework.

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

[](#installation)

```
composer require elegantweb/sanitizer
```

Usage
-----

[](#usage)

```
use Elegant\Sanitizer\Sanitizer;

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

$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',
];

$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'],
];
```

### 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 a string**empty\_string\_to\_null**If the given string is empty set it to `null`**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 stringCustom 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

27

—

LowBetter than 49% of packages

Maintenance54

Moderate activity, may be stable

Popularity8

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity31

Early-stage or recently created project

 Bus Factor1

Top contributor holds 92% 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

Unknown

Total

1

Last Release

305d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/37189aaf375fec26fe169890278cf5c7185427a43bb941a8441e32153c3859ea?d=identicon)[xiaoyiqingz](/maintainers/xiaoyiqingz)

---

Top Contributors

[![sharifzadesina](https://avatars.githubusercontent.com/u/205923701?v=4)](https://github.com/sharifzadesina "sharifzadesina (46 commits)")[![korridor](https://avatars.githubusercontent.com/u/26689068?v=4)](https://github.com/korridor "korridor (2 commits)")[![onlime](https://avatars.githubusercontent.com/u/2759561?v=4)](https://github.com/onlime "onlime (1 commits)")[![xiaoyiqingz](https://avatars.githubusercontent.com/u/3917927?v=4)](https://github.com/xiaoyiqingz "xiaoyiqingz (1 commits)")

---

Tags

laravelinputsanitationinput filterinput sanitationinput sanitizertransform input

###  Code Quality

TestsPHPUnit

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/xiaoyiqingz-sanitizer/health.svg)](https://phpackages.com/packages/xiaoyiqingz-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)
