xiaoyiqingz/sanitizer
Composer 安装命令:
composer require xiaoyiqingz/sanitizer
包简介
Sanitization library for PHP and the Laravel framework.
README 文档
README
Sanitization library for PHP and the Laravel framework.
Installation
composer require elegantweb/sanitizer
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
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
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
The following filters are available out of the box:
| Filter | Description |
|---|---|
| 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 |
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
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
xiaoyiqingz/sanitizer 适用场景与选型建议
xiaoyiqingz/sanitizer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 54 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 07 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「input」 「laravel」 「sanitation」 「input filter」 「input sanitation」 「input sanitizer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 xiaoyiqingz/sanitizer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 xiaoyiqingz/sanitizer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 xiaoyiqingz/sanitizer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Yii2 map input widget. Allows you to select geographcal coordinates via a human-friendly inteface.
Widget to add a remaining character counter to text inputs and textareas
Data sanitizer for PHP with built-in Laravel support.
Native (browser) color input field for SilverStripe
Data sanitizer forked from Waavi/Sanitaize with support for Laravel 5 to 8. .
Data sanitizer and Laravel 7 form requests with input sanitation.
统计信息
- 总下载量: 54
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-07-11