league / html-to-markdown
An HTML-to-markdown conversion helper for PHP
Fund package maintenance!
Requires
- php: ^7.2.5 || ^8.0
- ext-dom: *
- ext-xml: *
Requires (Dev)
- mikehaertl/php-shellcommand: ^1.1.0
- phpstan/phpstan: ^1.8.8 || ^2.0
- phpunit/phpunit: ^8.5 || ^9.2 || ^10.5 || ^11.0 || ^12.0 || ^13.0
- scrutinizer/ocular: ^1.6
- unleashedtech/php-coding-standard: ^2.7 || ^3.0
- vimeo/psalm: ^4.22 || ^5.0 || ^6.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
- dev-master / 5.2.x-dev
- 5.1.3
- 5.1.2
- 5.1.1
- 5.1.0
- 5.0.2
- 5.0.1
- 5.0.0
- 4.10.0
- 4.9.1
- 4.9.0
- 4.8.3
- 4.8.2
- 4.8.1
- 4.8.0
- 4.7.0
- 4.6.2
- 4.6.1
- 4.6.0
- 4.5.0
- 4.4.1
- 4.4.0
- 4.3.1
- 4.3.0
- 4.2.2
- 4.2.1
- 4.2.0
- 4.1.1
- 4.1.0
- 4.0.1
- 4.0.0
- 3.1.1
- 3.1.0
- 3.0.0
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.1
- 2.0.0
- dev-prep-5.1.3
- dev-fix-pipelines
- dev-convert-lists-to-four-spaces
- dev-feature-preserve-attributes
This package is auto-updated.
Last update: 2026-10-06 01:12:36 UTC
README
Library which converts HTML to Markdown for your sanity and convenience.
Requires: PHP 7.2+
Lead Developer: @colinodell
Original Author: @nickcernis
Why convert HTML to Markdown?
"What alchemy is this?" you mutter. "I can see why you'd convert Markdown to HTML," you continue, already labouring the question somewhat, "but why go the other way?"
Typically you would convert HTML to Markdown if:
- You have an existing HTML document that needs to be edited by people with good taste.
- You want to store new content in HTML format but edit it as Markdown.
- You want to convert HTML email to plain text email.
- You know a guy who's been converting HTML to Markdown for years, and now he can speak Elvish. You'd quite like to be able to speak Elvish.
- You just really like Markdown.
How to use it
Require the library by issuing this command:
composer require league/html-to-markdown
Add require 'vendor/autoload.php'; to the top of your script.
Next, create a new HtmlConverter instance, passing in your valid HTML code to its convert() function:
use League\HTMLToMarkdown\HtmlConverter; $converter = new HtmlConverter(); $html = "Quick, to the Batpoles!
"; $markdown = $converter->convert($html);
The $markdown variable now contains the Markdown version of your HTML as a string:
echo $markdown; // ==> ### Quick, to the Batpoles!
The included demo directory contains an HTML->Markdown conversion form to try out.
Conversion options
Caution
By default, this library preserves HTML tags without Markdown equivalents, like To strip HTML tags that don't have a Markdown equivalent while preserving the content inside them, set Or more explicitly, like this: Note that only the tags themselves are stripped, not the content they hold. To strip tags and their content, pass a space-separated list of tags in By default, all comments are stripped from the content. To preserve them, use the To preserve only specific comments, set By default, placeholder links are preserved. To strip the placeholder links, use the By default bold tags are converted using the asterisk syntax, and italic tags are converted using the underlined syntax. Change these by using the By default, test By default, You can pass current Support for Markdown tables is not enabled by default because it is not part of the original Markdown syntax. To use tables add the converter explicitly: Setext (underlined) headers are the default for H1 and H2. If you prefer the ATX style for H1 and H2 (# Header 1 and ## Header 2), set Headers of H3 priority and lower always use atx style. Links and images are referenced inline. Footnote references (where image src and anchor href attributes are listed in the footnotes) are not used. Blockquotes aren't line wrapped – it makes the converted Markdown easier to edit. HTML To Markdown requires PHP's xml, lib-xml, and dom extensions, all of which are enabled by default on most distributions. Errors such as "Fatal error: Class 'DOMDocument' not found" on distributions such as CentOS that disable PHP's xml extension can be resolved by installing php-xml. Many thanks to all contributors so far. Further improvements and feature suggestions are very welcome. HTML To Markdown creates a DOMDocument from the supplied HTML, walks through the tree, and converts each node to a text node containing the equivalent markdown, starting from the most deeply nested node and working inwards towards the root node. Use one of these great libraries: No guarantees about the Elvish, though., , , etc. If you will be parsing untrusted input from users, please consider setting the strip_tags and/or remove_nodes options documented below, and also using a library (like HTML Purifier) to provide additional HTML filtering.
strip_tags to true, like this:$converter = new HtmlConverter(array('strip_tags' => true));
$html = 'Turnips!';
$markdown = $converter->convert($html); // $markdown now contains "Turnips!"
$converter = new HtmlConverter();
$converter->getConfig()->setOption('strip_tags', true);
$html = 'Turnips!';
$markdown = $converter->convert($html); // $markdown now contains "Turnips!"
remove_nodes, like this:$converter = new HtmlConverter(array('remove_nodes' => 'span div'));
$html = 'Turnips!
preserve_comments option, like this:$converter = new HtmlConverter(array('preserve_comments' => true));
$html = 'Turnips!';
$markdown = $converter->convert($html); // $markdown now contains "Turnips!"
preserve_comments with an array of strings, like this:$converter = new HtmlConverter(array('preserve_comments' => array('Eggs!')));
$html = 'Turnips!';
$markdown = $converter->convert($html); // $markdown now contains "Turnips!"
strip_placeholder_links option, like this:$converter = new HtmlConverter(array('strip_placeholder_links' => true));
$html = 'Github';
$markdown = $converter->convert($html); // $markdown now contains "Github"
Style options
bold_style and italic_style options.$converter = new HtmlConverter();
$converter->getConfig()->setOption('italic_style', '*');
$converter->getConfig()->setOption('bold_style', '__');
$html = 'Italic and a bold';
$markdown = $converter->convert($html); // $markdown now contains "*Italic* and a __bold__"
Line break options
br tags are converted to two spaces followed by a newline character as per traditional Markdown. Set hard_break to true to omit the two spaces, as per GitHub Flavored Markdown (GFM).$converter = new HtmlConverter();
$html = '
line breakAutolinking options
a tags are converted to the easiest possible link syntax, i.e. if no text or title is available, then the syntax will be used rather than the full [url](url) syntax. Set use_autolinks to false to change this behavior to always use the full link syntax.$converter = new HtmlConverter();
$html = '';
$converter->getConfig()->setOption('use_autolinks', true);
$markdown = $converter->convert($html); // $markdown now contains "
Passing custom Environment object
Environment object to customize i.e. which converters should be used.$environment = new Environment(array(
// your configuration here
));
$environment->addConverter(new HeaderConverter()); // optionally - add converter manually
$converter = new HtmlConverter($environment);
$html = '
Header
';
$markdown = $converter->convert($html); // $markdown now contains "### Header" and "
"
Table support
use League\HTMLToMarkdown\HtmlConverter;
use League\HTMLToMarkdown\Converter\TableConverter;
$converter = new HtmlConverter();
$converter->getEnvironment()->addConverter(new TableConverter());
$html = "
";
$markdown = $converter->convert($html);A a Limitations
Style notes
header_style to 'atx' in the options array when you instantiate the object:$converter = new HtmlConverter(array('header_style'=>'atx'));Dependencies
Contributors
How it works
To-do
style).Trying to convert Markdown to HTML?