PHPackages                             saeven/zf2-purifier - 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. [Search &amp; Filtering](/categories/search)
4. /
5. saeven/zf2-purifier

ActiveLibrary[Search &amp; Filtering](/categories/search)

saeven/zf2-purifier
===================

HTML Purifier as filter and view helper for Zend Framework 2.5

0.1.2(10y ago)0141BSD-3-ClausePHPPHP &gt;=5.3.3.

Since Apr 23Pushed 9y ago1 watchersCompare

[ Source](https://github.com/Saeven/Purifier)[ Packagist](https://packagist.org/packages/saeven/zf2-purifier)[ Docs](https://github.com/Soflomo/Purifier)[ RSS](/packages/saeven-zf2-purifier/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (5)Used By (0)

Soflomo\\Purifier
=================

[](#soflomopurifier)

Soflomo\\Purifier is the [HTMLPurifier](http://htmlpurifier.org/) integration for Zend Framework 2. It provides a `Zend\Filter\FilterInterface` instance so you can use Soflomo\\Purifier directly to filter your html input with your `Zend\InputFilter` classes. Furthermore, a view helper is provided to help purifying html on the fly in view scripts.

Installation
------------

[](#installation)

`Soflomo\Purifier` is available through composer. Add "soflomo/purifier" to your composer.json list. You can specify the latest stable version of `Soflomo\Purifier`:

```
"soflomo/purifier": ">=0.1.0"

```

To enable the module in your `config/application.config.php` file, add an entry `Soflomo\Purifier` to the list of enabled modules.

Usage
-----

[](#usage)

In your input filter configuration, add the `htmlpurifier` filter to filter the result. An example form:

```
namespace Application\Form;

use Zend\InputFilter;
use Zend\Form\Form;

class Foo extends Form implements
    InputFilter\InputFilterProviderInterface
{
    public function __construct($name = null)
    {
        parent::__construct($name);

        $this->add(array(
            'name'    => 'title',
            'options' => array(
                'label' => 'Title'
            ),
        ));

        $this->add(array(
            'name'    => 'text',
            'options' => array(
                'label' => 'Text'
            ),
            'attributes' => array(
                'type'  => 'textarea',
            ),
        ));
    }

    public function getInputFilterSpecification()
    {
        return array(
            'title' => array(
                'required' => true,
                'filters'  => array(
                    array('name' => 'stringtrim'),
                ),
            ),
            'text'  => array(
                'required' => true,
                'filters'  => array(
                    array('name' => 'stringtrim'),
                    array('name' => 'htmlpurifier'),
                ),
            ),
        );
    }
}
```

If your form has the filter plugin manager injected, this should work out-of-the-box. If not, please read [how to inject the filter plugin manager](#inject-filter-manager).

When you are not able to filter the html when it is inserted into the database, you might want to filter the html in your view. Please be aware HTMLPurifier is not a very fast library and as such, filtering on every request can be a significant performance bottleneck. Be advised to use a caching mechanism to cache the output of the filtered html. The view helper is available under the key `htmlPurifier`:

```

```

And there is a shorthand available too:

```

```

Inject Filter Manager
---------------------

[](#inject-filter-manager)

If you instantiate your form with `new FormClass`, the plugin manager for filters is not injected. As such, you get a `ServiceNotFoundException`: "Zend\\Filter\\FilterPluginManager::get was unable to fetch or create an instance for htmlpurifier". This means the filter plugin manager does not know about the `htmlpurifier` plugin.

You can fix this by grabbing the filter plugin manager from the Service Manager and inject it into the form manually:

```
use Zend\Filter\FilterChain;

$form    = new MyForm;

// $sl is the service locator
$plugins = $sl->get('FilterManager');
$chain   = new FilterChain;
$chain->setPluginManager($plugins);

$form->getFormFactory()->getInputFilterFactory()->setDefaultFilterChain($chain);
```

The library [Soflomo\\Common](https://github.com/Soflomo/Common) has a utility class to inject the filter manager. If you require `soflomo\common` via composer, you can replace above code with this line:

```
use Soflomo\Common\Form\FormUtils;

$form = new MyForm;

// $sl is the service locator
FormUtils::injectFilterPluginManager($form, $sl);
```

Speed up performance
--------------------

[](#speed-up-performance)

HTMLPurifier is not the fastest library, but it is the most secure one to filter html in php. By default, HTMLPurifier uses a large number of classes and inclusion of all the files slows down performance during autoloading. You can [create a standalone version](http://htmlpurifier.org/live/INSTALL) of the HTMLPurifier class, where a single file contains most of the classes.

The script in `vendor/bin/purifier-generate-standalone` generates this file for you. The standalone file is created inside `vendor/ezyang/htmlpurifier/library` (there is nothing to configure about that) so make sure you can write in that directory. The Soflomo\\Purifier library helps you to use this standalone version by the configuration option `soflomo_prototype.standalone`. In your `config/autoload/local.php`:

```
return array(
    'soflomo_purifier' => array(
        'standalone' => true,
    ),
);
```

If your composer does not load the libraries into vendor/, you can update the path to the standalone version too:

```
return array(
    'soflomo_purifier' => array(
        'standalone'      => true,
        'standalone_path' => 'something-else/ezyang/htmlpurifier/library/HTMLPurifier.standalone.php',
    ),
);
```

Configure HTMLPurifier
----------------------

[](#configure-htmlpurifier)

The HTMLPurifier has a class `HTMLPurifier_Config` where it is possible to configure the purifier rules. Most configuration rules are based on a key/value pair: `$config->set('HTML.Doctype', 'HTML 4.01 Transitional');`. This mapping is copied to Zend Framework 2 configuration files, so you can easily modify HTMLPurifiers configation:

```
return array(
    'soflomo_purifier' => array(
        'config' => array(
            'HTML.Doctype' => 'HTML 4.01 Transitional'
        ),
    ),
);
```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~406 days

Total

3

Last Release

3963d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/887224?v=4)[Alexandre Lemaire](/maintainers/Saeven)[@Saeven](https://github.com/Saeven)

---

Top Contributors

[![Saeven](https://avatars.githubusercontent.com/u/887224?v=4)](https://github.com/Saeven "Saeven (8 commits)")

---

Tags

htmlPurifierfilterzf2purify

### Embed Badge

![Health badge](/badges/saeven-zf2-purifier/health.svg)

```
[![Health](https://phpackages.com/badges/saeven-zf2-purifier/health.svg)](https://phpackages.com/packages/saeven-zf2-purifier)
```

###  Alternatives

[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5325.6M19](/packages/stevebauman-purify)[htmlawed/htmlawed

Official htmLawed PHP library for HTML filtering

401.1M9](/packages/htmlawed-htmlawed)[stolz/laravel-html-tidy

HTML Tidy middleware for Laravel

268.7k](/packages/stolz-laravel-html-tidy)[lincanbin/white-html-filter

A lightweight php-based HTML tag and attribute whitelist filter.

1215.1k1](/packages/lincanbin-white-html-filter)[bakame/html-table

convert html table into a PHP data structure

113.0k](/packages/bakame-html-table)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
