Html2Text

PHP library that converts HTML markup into readable, formatted plain text

Library
Composer
v4.3.2
589stars
GPL-2.0-or-later

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
45/100Fair
Development Activity0
Maintenance20
Community80
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
56/100Fair
Architecture50
Code Quality60
Innovation40
Learning Curve75

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\Html2Text class 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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search