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

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

carbonclean/sanitizer
=====================

Sanitization library for PHP and the Laravel framework.

v1.0(4y ago)341MITPHPPHP ^7.4|^8.0

Since Aug 2Pushed 4y ago1 watchersCompare

[ Source](https://github.com/realashleybailey/CarbonClean)[ Packagist](https://packagist.org/packages/carbonclean/sanitizer)[ RSS](/packages/carbonclean-sanitizer/feed)WikiDiscussions main Synced today

READMEChangelog (1)Dependencies (4)Versions (2)Used By (0)

sanitizer
=========

[](#sanitizer)

[![GitHub release (latest by date)](https://camo.githubusercontent.com/aafef70c71cdc147137921bf390d016231a0a772f5a2041a62e8d1409b64b918/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f7265616c6173686c65796261696c65792f436172626f6e436c65616e3f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/aafef70c71cdc147137921bf390d016231a0a772f5a2041a62e8d1409b64b918/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f7265616c6173686c65796261696c65792f436172626f6e436c65616e3f7374796c653d666c61742d737175617265)[![GitHub Workflow Status](https://camo.githubusercontent.com/43f20abf3d4acf3ee2312e041336b420fb3713db33a51af06e4d0c7afec5fc68/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f7265616c6173686c65796261696c65792f436172626f6e436c65616e3f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/43f20abf3d4acf3ee2312e041336b420fb3713db33a51af06e4d0c7afec5fc68/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f7265616c6173686c65796261696c65792f436172626f6e436c65616e3f7374796c653d666c61742d737175617265)

> Sanitization library for PHP and the Laravel framework.

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

[](#installation)

```
composer require carbonclean/sanitizer
```

Usage
-----

[](#usage)

```
use CarbonClean\Sanitizer\Sanitizer;

$data = [
    'title' => ' ',
    'name' => ' John Doe ',
    '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' => 'John Doe',
    '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 CarbonClean\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 string**boolean**If the given value is not a boolean set it to `null`Custom Filters
--------------

[](#custom-filters)

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

```
class RemoveStringsFilter implements \CarbonClean\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);
```

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

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

1743d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/16636012?v=4)[Ashley Bailey](/maintainers/realashleybailey)[@realashleybailey](https://github.com/realashleybailey)

---

Top Contributors

[![realashleybailey](https://avatars.githubusercontent.com/u/16636012?v=4)](https://github.com/realashleybailey "realashleybailey (8 commits)")

---

Tags

laravelinputsanitationinput filterinput sanitationinput sanitizertransform input

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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