Html2Text
PHP library that converts HTML markup into readable, formatted plain text
Repository Health
Technical Analysis
Html2Text is a PHP library that parses HTML and produces a formatted plain-text rendering of it, handling links, lists, tables, blockquotes, and definition lists along the way. It’s commonly used to generate plain-text email alternatives from HTML templates or to strip rich formatting down to readable text for previews, notifications, or logging.
What You Get
- A single
Html2Text\Html2Textclass with a simple constructor-and-getText()API - Handling for common HTML elements: links (rendered with footnote-style references), lists, definition lists, tables, blockquotes, and preformatted text
- Configurable options for character encoding and output width via constructor options
- A full PHPUnit test suite covering individual element types (tables, images, spans, ins/del tags, etc.)
Common Use Cases
- Generating a plain-text alternative part for HTML emails to satisfy mail clients and spam filters that expect one
- Producing readable text previews or excerpts from HTML content for notifications, search indexes, or logs
- Stripping rich HTML formatting from user-generated or CMS content before further text processing
Under The Hood
Architecture - The library is implemented as a single class, Html2Text (src/Html2Text.php, ~670 lines), that takes an HTML string, loads it into a DOM document, and walks the DOM tree recursively, dispatching per-tag handling logic (links become bracketed references with a footnote list appended, tables are rendered with aligned columns, lists get bullet/number prefixes) to build up a plain-text string.
Tech Stack - Pure PHP with no required runtime dependencies beyond PHP’s built-in DOM extension; it optionally benefits from the mbstring extension (or the symfony/polyfill-mbstring package) for correct multi-byte character handling, and uses PHPUnit (v4 or ^9.0) for its test suite.
Code Quality - The test/ directory contains 16 dedicated PHPUnit test files, each targeting a specific HTML construct (tables, images, spans, definition lists, ins/del tags, search-and-replace behavior, string case handling), giving solid coverage of the library’s per-element conversion logic; the single-class design keeps the codebase easy to audit despite its many small tag-handling branches.
API Design - The API is deliberately minimal — construct an Html2Text object with an HTML string and options, then call a method to retrieve the converted plain text — which keeps integration to a couple of lines, though the single-class design means all conversion logic lives in one file rather than being split into per-element handlers.
Used by 3 apps in this directory
FreeScout
Customer Support
Run your own help desk and shared inbox — a fully self-hosted, open-source alternative to Zendesk and Help Scout with no per-agent fees.
LimeSurvey
Forms Surveys
The world's most flexible open-source survey platform with 900+ templates, conditional logic, 80+ languages, and full GDPR compliance for any scale.
wallabag
Bookmarks Archiving
Self-hosted read-it-later app that saves clean, ad-free articles from any webpage for distraction-free reading across all your devices.