hirasso / html-processor
A tiny HTML processor written in PHP 🐘
Fund package maintenance!
v2.1.1
2026-06-19 21:03 UTC
Requires
- php: >=8.4
- asika/autolink: >=2.2
- symfony/var-dumper: >=6.4
Requires (Dev)
- laravel/pint: ^1.20
- pestphp/pest: ^3.7
- pestphp/pest-plugin-watch: ^3.0
- phpstan/phpstan: ^2.1
This package is auto-updated.
Last update: 2026-06-19 21:05:12 UTC
README
A tiny HTML processor written in PHP 🐘
Features (all optional)
- Automatically convert raw URLs to links
- Remove empty elements
- Process anchor
aelements:- Add classes reflecting the link type (e.g.
link--external link--file) - Open external links in a new tab
- Add classes reflecting the link type (e.g.
- Obfuscate email addresses and phone numbers to confuse spam bots (see this article)
- Automatically link prefixed words (e.g.
@mentionor#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.