PHPackages                             nicofff/ammonia - 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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. nicofff/ammonia

ActivePhp-ext[Validation &amp; Sanitization](/categories/validation)

nicofff/ammonia
===============

Rust-based HTML sanitizer for PHP — wraps the ammonia crate

v0.3.0(3mo ago)12MITRustPHP &gt;=8.2CI passing

Since Mar 8Pushed 3mo agoCompare

[ Source](https://github.com/nicofff/ammonia-php-rs)[ Packagist](https://packagist.org/packages/nicofff/ammonia)[ Docs](https://github.com/nicofff/ammonia-php-rs)[ RSS](/packages/nicofff-ammonia/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (3)DependenciesVersions (4)Used By (0)

ammonia-php-rs
==============

[](#ammonia-php-rs)

Rust-based HTML sanitizer for PHP — wraps the [ammonia](https://github.com/rust-ammonia/ammonia) crate.

10-30x faster than HTMLPurifier with comparable security defaults.

Install via PIE
---------------

[](#install-via-pie)

Requires [PIE](https://github.com/php/pie) 1.4.0+:

```
pie install nicofff/ammonia
```

Usage
-----

[](#usage)

### Quick sanitize (defaults)

[](#quick-sanitize-defaults)

```
use function Ammonia\sanitize_html;

$clean = sanitize_html('Hello alert("xss") world');
// Output: Hello  world
```

### Builder API (custom configuration)

[](#builder-api-custom-configuration)

```
use Ammonia\Builder;

$clean = (new Builder())
    ->addTags(['custom-element'])
    ->rmTags(['img'])
    ->addGenericAttributes(['class', 'id'])
    ->addTagAttributes('a', ['href', 'target'])
    ->addAllowedClasses('div', ['container', 'wrapper'])
    ->stripComments(true)
    ->linkRel('noopener noreferrer')
    ->idPrefix('user-content-')
    ->clean($dirtyHtml);
```

### Available Builder methods

[](#available-builder-methods)

All methods (except `clean()`) return `$this` for chaining.

MethodDescription`tags(string[])`Replace entire allowed tag list`addTags(string[])`Add tags to whitelist`rmTags(string[])`Remove tags from whitelist`cleanContentTags(string[])`Replace content-stripping tag list`addCleanContentTags(string[])`Add content-stripping tags`rmCleanContentTags(string[])`Remove content-stripping tags`genericAttributes(string[])`Replace allowed attributes (all tags)`addGenericAttributes(string[])`Add allowed attributes`rmGenericAttributes(string[])`Remove allowed attributes`addTagAttributes(string, string[])`Add attributes for a specific tag`rmTagAttributes(string, string[])`Remove attributes for a specific tag`urlSchemes(string[])`Replace allowed URL schemes`addUrlSchemes(string[])`Add URL schemes`rmUrlSchemes(string[])`Remove URL schemes`addAllowedClasses(string, string[])`Add allowed CSS classes for a tag`rmAllowedClasses(string, string[])`Remove allowed CSS classes`stripComments(bool)`Strip HTML comments (default: true)`linkRel(?string)`Set rel attribute on links (null to disable)`idPrefix(?string)`Prefix all id attribute values`clean(string): string`Sanitize HTML and return resultIDE Support
-----------

[](#ide-support)

PHP stubs for autocompletion and static analysis are attached to each [GitHub release](https://github.com/nicofff/ammonia-php-rs/releases) as `ammonia.stubs.php`. Compatible with PhpStorm, Intelephense (VS Code), PHPStan, and Psalm.

Building from source
--------------------

[](#building-from-source)

```
cargo build --release
php -d extension=./target/release/libammonia_php_rs.dylib -r 'echo Ammonia\sanitize_html("hi");'
```

Running benchmarks
------------------

[](#running-benchmarks)

```
cd benchmark
composer install
cd ..
php -d extension=./target/release/libammonia_php_rs.dylib benchmark/benchmark.php
```

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance80

Actively maintained with recent releases

Popularity4

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity39

Early-stage or recently created project

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 ~0 days

Total

3

Last Release

106d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/763439?v=4)[Nicolas Far](/maintainers/nicofff)[@nicofff](https://github.com/nicofff)

### Embed Badge

![Health badge](/badges/nicofff-ammonia/health.svg)

```
[![Health](https://phpackages.com/badges/nicofff-ammonia/health.svg)](https://phpackages.com/packages/nicofff-ammonia)
```

###  Alternatives

[chaoswey/taiwan-id-validator

台灣身分證、統一編號驗證

319.9k](/packages/chaoswey-taiwan-id-validator)

PHPackages © 2026

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