Package

Stevebauman Purify

An HTML Purifier / Sanitizer for Laravel

Package

stevebauman/purify

View on GitHub →

Laravel Purify Package Summary

Laravel Purify is a package that provides a simple Laravel wrapper around the HTMLPurifier library, enabling developers to clean and filter HTML inputs within their Laravel applications effectively.

Key Features:

  • Flexible Cleaning: Purify allows cleaning of individual strings or arrays of HTML content, ensuring that your application is safe from XSS attacks while preserving the necessary HTML tags.
  • Dynamic Configuration: Users can specify different HTMLPurifier configurations on-the-fly, adapting to various content filtering needs across different parts of the application.
  • Custom HTML Definitions: Extend the built-in HTML definitions or create custom definitions to accommodate specific HTML elements and attributes.
  • Caching Mechanisms: Optimizes performance by caching definitions, with support for file-based and cache-based storage mechanisms.
  • Eloquent Model Casting: Utilizes custom casting to sanitize data automatically when retrieving or setting model attributes.
  • Support for HTML5 and Custom Tags: Includes HTML5 support not originally available in HTMLPurifier and allows for the customization to support unique tags like those from the Basecamp Trix editor.

Installation:

To install Purify, use Composer:

composer require stevebauman/purify

After installation, publish the configuration file:

php artisan vendor:publish --provider="Stevebauman\Purify\PurifyServiceProvider"

Usage Examples:

Cleaning a String:

use Stevebauman\Purify\Facades\Purify;

$input = '<script>alert("bad");</script> <p>Good</p>';
$cleaned = Purify::clean($input); // Returns '<p>Good</p>'

Cleaning an Array:

$dirtyArray = ['<script>alert("bad");</script>', '<p>Good</p>'];
$cleanedArray = Purify::clean($dirtyArray);

Using Custom Configuration:

$cleaned = Purify::config('custom')->clean($input);

Configuration:

Modify the config/purify.php file to adjust HTMLPurifier settings, define custom rules, or manage caching preferences.

Cache Management:

To clear the HTMLPurifier cache, particularly after configuration changes:

php artisan purify:clear

Upgrading:

Steps are provided within the package's README for upgrading from older versions, ensuring a smooth transition and functionality retention.

Overall, Laravel Purify is an essential tool for developers needing robust HTML sanitization solutions, providing ease of use, configurability, and integration with Laravel's features such as Eloquent models.

Stevebauman Purify Related Articles

Dynamic Cache, Database, and Mail Builders in Laravel 11.31 image

Dynamic Cache, Database, and Mail Builders in Laravel 11.31

Read article
"In Array Keys" Validation Rule Added in Laravel 12.16 image

"In Array Keys" Validation Rule Added in Laravel 12.16

Read article
URI Parsing and Mutation in Laravel 11.35 image

URI Parsing and Mutation in Laravel 11.35

Read article
Use Laravel Dusk to Test JS-Based Applications image

Use Laravel Dusk to Test JS-Based Applications

Read article
Access Request Data Fluently in Laravel 11.34 image

Access Request Data Fluently in Laravel 11.34

Read article
Remove "final" keywords from classes and methods in vendor packages with Unfinalize image

Remove "final" keywords from classes and methods in vendor packages with Unfinalize

Read article

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech

Let your users buy domains without leaving your app

If your Laravel app gives users a site, a storefront, or a workspace, they’ll eventually want their own domain. The name.com API lets them search, register, and connect one without leaving your product. It’s a clean REST API, so Laravel’s HTTP client is all you need.

Let your users buy domains without leaving your app

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum