PHPackages                             tobento/app-html-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. tobento/app-html-sanitizer

ActiveLibrary

tobento/app-html-sanitizer
==========================

App html sanitizer to sanitize untrusted HTML code.

2.0.1(6mo ago)0461MITPHPPHP &gt;=8.4

Since Dec 7Pushed 6mo ago1 watchersCompare

[ Source](https://github.com/tobento-ch/app-html-sanitizer)[ Packagist](https://packagist.org/packages/tobento/app-html-sanitizer)[ Docs](https://www.tobento.ch)[ RSS](/packages/tobento-app-html-sanitizer/feed)WikiDiscussions 2.x Synced 1mo ago

READMEChangelog (4)Dependencies (8)Versions (6)Used By (1)

App HTML Sanitizer
==================

[](#app-html-sanitizer)

App HTML Sanitizer to sanitize untrusted HTML code.

Table of Contents
-----------------

[](#table-of-contents)

- [Getting Started](#getting-started)
    - [Requirements](#requirements)
- [Documentation](#documentation)
    - [App](#app)
    - [Sanitizer Boot](#sanitizer-boot)
        - [Sanitizer Config](#sanitizer-config)
    - [Basic Usage](#basic-usage)
        - [Sanitizing HTML](#sanitizing-html)
        - [Sanitizing HTML in Views](#sanitizing-html-in-views)
        - [Sanitizing HTML using Function](#sanitizing-html-using-function)
    - [Available Sanitizers](#available-sanitizers)
        - [Purifier Sanitizer](#purifier-sanitizer)
        - [Symfony Sanitizer](#symfony-sanitizer)
    - [Adding Sanitizers](#adding-sanitizers)
- [Credits](#credits)

---

Getting Started
===============

[](#getting-started)

Add the latest version of the app HTML Sanitizer project running this command.

```
composer require tobento/app-html-sanitizer

```

Requirements
------------

[](#requirements)

- PHP 8.4 or greater

Documentation
=============

[](#documentation)

App
---

[](#app)

Check out the [**App Skeleton**](https://github.com/tobento-ch/app-skeleton) if you are using the skeleton.

You may also check out the [**App**](https://github.com/tobento-ch/app) to learn more about the app in general.

Sanitizer Boot
--------------

[](#sanitizer-boot)

The sanitizer boot does the following:

- installs and loads html sanitizer config file
- implements html sanitizer interfaces

```
use Tobento\App\AppFactory;
use Tobento\App\HtmlSanitizer\HtmlSanitizerInterface;
use Tobento\App\HtmlSanitizer\HtmlSanitizersInterface;

// Create the app
$app = new AppFactory()->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');

// Adding boots:
$app->boot(\Tobento\App\HtmlSanitizer\Boot\HtmlSanitizer::class);
$app->booting();

// Implemented interfaces:
$htmlSanitizer = $app->get(HtmlSanitizerInterface::class);
$htmlSanitizers = $app->get(HtmlSanitizersInterface::class);

// Run the app
$app->run();
```

### Sanitizer Config

[](#sanitizer-config)

The configuration for the sanitizer is located in the `app/config/html_sanitizer.php` file at the default App Skeleton config location where you can configure sanitizers for your application.

Basic Usage
-----------

[](#basic-usage)

### Sanitizing HTML

[](#sanitizing-html)

```
use Tobento\App\HtmlSanitizer\HtmlSanitizerInterface;

$htmlSanitizer = $app->get(HtmlSanitizerInterface::class);

$safeHtml = $htmlSanitizer->sanitize(html: $html);

$safeHtml = $htmlSanitizer->sanitizeFor(element: 'h1' html: $html);
```

**Using Specific Sanitizer**

```
use Tobento\App\HtmlSanitizer\HtmlSanitizersInterface;

$htmlSanitizers = $app->get(HtmlSanitizersInterface::class);

$htmlSanitizer = $htmlSanitizers->get(name: 'custom');

$safeHtml = $htmlSanitizer->sanitize(html: $html);

$safeHtml = $htmlSanitizer->sanitizeFor(element: 'h1' html: $html);
```

### Sanitizing HTML in Views

[](#sanitizing-html-in-views)

If you have installed the [App View](https://github.com/tobento-ch/app-view), you may use the `sanitizeHtml` and `sanitizeHtmlFor` view macro to sanitize untrusted HTML:

```
