phpsanitization/phpsanitization
Composer 安装命令:
composer require phpsanitization/phpsanitization
包简介
Modern PHP Sanitization Library
关键字:
README 文档
README
PhpSanitization
About
A modern, type-safe PHP sanitization library designed for security and efficiency.
PhpSanitization provides robust validation and sanitization capabilities to ensure your data is clean and safe. The library implements strict typing and leverages PHP 8's features for enhanced type safety and performance.
It can process strings, arrays (including deeply nested structures), and protect against various security threats like XSS and SQL injection.
Features
- Strict Typing: Full type declarations for all methods and parameters
- Enhanced Security: Improved protections against XSS and SQL injection attacks
- Recursive Sanitization: Deep cleaning of nested arrays and complex data structures
- Method Chaining: Fluent interface for composing multiple operations
- Enhanced Email Validation: DNS checking and custom provider validation
- Improved SQL Escaping: Better protection using
strtr()for more secure queries - Comprehensive Documentation: Complete examples for all features
- PHP 8 Features: Utilizes union types and match expressions
Requirements
- PHP 8.0+
- Composer
Installation & Usage
Installation
$ composer require phpsanitization/phpsanitization
Basic Setup
<?php declare(strict_types=1); require_once 'vendor/autoload.php'; use PhpSanitization\PhpSanitization\Sanitization; use PhpSanitization\PhpSanitization\Utils; // Initialize with proper dependency injection $sanitizer = new Sanitization(new Utils());
Core Features
String Sanitization
// Sanitize a string with potential XSS $result = $sanitizer->useSanitize("<script>alert('xss');</script>"); echo $result; // Outputs safely encoded HTML entities
Array Sanitization
// Sanitize a simple array $array = [ "<script>alert('xss');</script>", "<a href='javascript:alert(\"click\")'>Click me</a>" ]; $result = $sanitizer->useSanitize($array); // Sanitize an associative array $assocArray = [ "name" => "<script>alert('name');</script>", "url" => "<a href='javascript:alert(\"url\")'>URL</a>" ]; $result = $sanitizer->useSanitize($assocArray);
Recursive Array Sanitization (New in v2.0)
// Sanitize deeply nested structures $nestedData = [ 'user' => [ 'name' => 'John <script>alert("XSS")</script> Doe', 'settings' => [ 'theme' => 'dark<iframe src="malicious.html">' ] ] ]; $sanitizedData = $sanitizer->useSanitize($nestedData); // All levels sanitized!
SQL Query Escaping
// Escape a SQL query to prevent injection $query = "SELECT * FROM `users` WHERE `username` = 'admin' OR 1=1--'"; $safeQuery = $sanitizer->useEscape($query);
Enhanced Email Validation (Improved in v2.0)
// Basic email validation with DNS checking $isValid = $sanitizer->validateEmail("user@example.com"); // Email validation with custom provider list $customProviders = ['company.com', 'organization.org']; $isValid = $sanitizer->validateEmail("user@company.com", $customProviders); // Email validation without DNS checking (for testing) $isValid = $sanitizer->validateEmail("test@example.com", [], false);
Method Chaining (New in v2.0)
// Chain multiple operations together $result = $sanitizer ->setData("<script>alert('XSS');</script>") ->useSanitize(); // Process with callback $sanitizer ->setData("<p>Some content with <script>alert('danger');</script></p>") ->useSanitize(); $processed = $sanitizer->callback(function($data) { return "Processed: " . $data; }, $sanitizer->getData());
Utility Methods
// Check if a variable is empty $isEmpty = $sanitizer->utils->isEmpty($variable); // Check if an array is associative $isAssoc = $sanitizer->utils->isAssociative($array); // Validate using filter_var $isValidIP = $sanitizer->isValid("127.0.0.1", FILTER_VALIDATE_IP);
Custom Processing with Callbacks
// Use callbacks for custom processing $result = $sanitizer->callback(function($data) { // Custom processing logic here return strtoupper($data) . " (processed)"; }, "input data");
Running the Examples
The library includes a comprehensive set of examples demonstrating all features:
# Navigate to the examples directory cd examples # Run the examples using PHP's built-in server php -S localhost:8000
Then visit http://localhost:8000 in your browser to see all examples in action.
Migration from v1.x to v2.0
Version 2.0 introduces several breaking changes to improve security and type safety:
- Strict typing is now enforced with
declare(strict_types=1) - Private methods: Several helper methods are now private (use public methods instead)
- Utils class separation: Utility methods moved to a separate class
- Type declarations: All methods now have parameter and return type declarations
- Method signatures: Some method signatures have changed to support new features
If you're upgrading from 1.x, review the examples directory for guidance on updating your code.
Documentation
Comprehensive documentation is available in the examples directory and in the source code. For full API documentation, visit PhpSanitization Documentation.
Changelog
Please see CHANGELOG.md for a detailed list of changes in each version.
Contributing
Please have a look at CONTRIBUTING.md.
Code of Conduct
Please have a look at CODE_OF_CONDUCT.md.
License
This package is licensed using the MIT License.
Please have a look at LICENSE.md.
Copyright
Copyright (c) Faris Alotaibi - 2025
phpsanitization/phpsanitization 适用场景与选型建议
phpsanitization/phpsanitization 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.76k 次下载、GitHub Stars 达 18, 最近一次更新时间为 2022 年 01 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「security」 「filter」 「sanitize」 「filtering」 「sanitization」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 phpsanitization/phpsanitization 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 phpsanitization/phpsanitization 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 phpsanitization/phpsanitization 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Nova searchable filter for belongsTo relationships.
Provide a way to secure accesses to all routes of an symfony application.
Symfony DataGridBundle
It's a barebone security class written on PHP
Data provider for yii2
This Laravel Nova package allows you to detach filters from the filter dropdown
统计信息
- 总下载量: 1.76k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 18
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-01-10