hirasso/html-processor

A tiny HTML processor written in PHP 🐘

Maintainers

Package info

github.com/hirasso/html-processor

pkg:composer/hirasso/html-processor

Fund package maintenance!

hirasso

Statistics

Installs: 75

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 1

v2.1.1 2026-06-19 21:03 UTC

README

Latest Version on Packagist Test Status Code Coverage

A tiny HTML processor written in PHP 🐘

Features (all optional)

  • Automatically convert raw URLs to links
  • Remove empty elements
  • Process anchor a elements:
    • Add classes reflecting the link type (e.g. link--external link--file)
    • Open external links in a new tab
  • Obfuscate email addresses and phone numbers to confuse spam bots (see this article)
  • Automatically link prefixed words (e.g. @mention or #hashtag) to a URL of your choice
  • Strip tags
  • Conditionally apply any operation

Promises

  • Fluent API
  • Fully compatible with HTML5
  • All mutations are lazily queued and processed in one go
  • Extensively tested

Installation

composer require hirasso/html-processor

Minimal Example

use function Hirasso\HTMLProcessor\process;

echo process($html)->obfuscateContacts();

Maximal Example

use function Hirasso\HTMLProcessor\process;

echo process($html)
    ->autolinkUrls()
    ->removeEmptyElements('p')
    ->obfuscateContacts(email: true, phone: true)
    ->processLinks(fn ($link) => $link->addClasses()->openExternalInNewTab())
    ->autolinkPrefix('@', 'https://your-instance.social/@')
    ->autolinkPrefix('#', 'https://your-instance.social/tags')
    // ->when() accepts a bool or a closure as the condition:
    ->when($isRichText, fn ($p) => $p->stripTags(allowedTags: ['p', 'a', 'strong', 'em']));

→ Browse the tests folder for more usage examples.