voku / html2text
Only a Fork of -> html2text: Converts HTML to formatted plain text
Requires
- php: >=7.1.0
- voku/portable-utf8: ^6.0
Requires (Dev)
- phpunit/phpunit: ^7.5 || ^8.5 || ^9.6
This package is auto-updated.
Last update: 2026-05-20 15:35:54 UTC
README
📝 Html2Text
Description
Convert HTML to formatted plain text, e.g. for text mails.
Installation
The recommended installation way is through Composer.
$ composer require voku/html2text
Basic Usage
$html = new \voku\Html2Text\Html2Text('Hello, "<b>world</b>"'); echo $html->getText(); // Hello, "WORLD"
Extended Usage
Each element (h1, li, div, etc) can have the following options:
- 'case' => convert case (
Html2Text::OPTION_NONE, Html2Text::OPTION_UPPERCASE, Html2Text::OPTION_LOWERCASE , Html2Text::OPTION_UCFIRST, Html2Text::OPTION_TITLE) - 'prepend' => prepend a string
- 'append' => append a string
For example:
$html = '<h1>Should have "AAA" changed to BBB</h1><ul><li>• Custom bullet should be removed</li></ul><img alt="The Linux Tux" src="tux.png" />'; $expected = 'SHOULD HAVE "BBB" CHANGED TO BBB' . "\n\n" . '- Custom bullet should be removed |' . "\n\n" . '[IMAGE]: "The Linux Tux"'; $html2text = new Html2Text( $html, array( 'width' => 0, 'elements' => array( 'h1' => array( 'case' => Html2Text::OPTION_UPPERCASE, 'replace' => array('AAA', 'BBB')), 'li' => array( 'case' => Html2Text::OPTION_NONE, 'replace' => array('•', ''), 'prepend' => "- ", 'append' => " |", ), ), ) ); $html2text->setPrefixForImages('[IMAGE]: '); $html2text->setPrefixForLinks('[LINKS]: '); $html2text->getText(); // === $expected
Live Demo
History
This library started life on the blog of Jon Abernathy http://www.chuggnutt.com/html2text
A number of projects picked up the library and started using it - among those was RoundCube mail. They made a number of updates to it over time to suit their webmail client.
Now this is a extend fork of the original Html2Text.
Support
For support and donations please visit Github | Issues | PayPal | Patreon.
For status updates and release announcements please visit Releases | Twitter | Patreon.
For professional support please contact me.
Thanks
- Thanks to GitHub (Microsoft) for hosting the code and a good infrastructure including Issues-Managment, etc.
- Thanks to IntelliJ as they make the best IDEs for PHP and they gave me an open source license for PhpStorm!
- Thanks to GitHub Actions for the automated CI pipeline.
- Thanks to StyleCI for the simple but powerful code style check.
- Thanks to PHPStan && Psalm for really great Static analysis tools and for discover bugs in the code!