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.
- 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
- PHP 8.0+
- Composer
$ composer require phpsanitization/phpsanitization
<?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());
// Sanitize a string with potential XSS
$result = $sanitizer->useSanitize("<script>alert('xss');</script>");
echo $result; // Outputs safely encoded HTML entities
// 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);
// 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!
// Escape a SQL query to prevent injection
$query = "SELECT * FROM `users` WHERE `username` = 'admin' OR 1=1--'";
$safeQuery = $sanitizer->useEscape($query);
// Basic email validation with DNS checking
$isValid = $sanitizer->validateEmail("[email protected]");
// Email validation with custom provider list
$customProviders = ['company.com', 'organization.org'];
$isValid = $sanitizer->validateEmail("[email protected]", $customProviders);
// Email validation without DNS checking (for testing)
$isValid = $sanitizer->validateEmail("[email protected]", [], false);
// 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());
// 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);
// Use callbacks for custom processing
$result = $sanitizer->callback(function($data) {
// Custom processing logic here
return strtoupper($data) . " (processed)";
}, "input data");
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.
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.
Comprehensive documentation is available in the examples directory and in the source code. For full API documentation, visit PhpSanitization Documentation.
Please see CHANGELOG.md
for a detailed list of changes in each version.
Please have a look at CONTRIBUTING.md
.
Please have a look at CODE_OF_CONDUCT.md
.
This package is licensed using the MIT License.
Please have a look at LICENSE.md
.
Copyright (c) Faris Alotaibi - 2025