Convert HTML to Markdown with PHP

Overview

HTML To Markdown for PHP

Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

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:

  1. You have an existing HTML document that needs to be edited by people with good taste.
  2. You want to store new content in HTML format but edit it as Markdown.
  3. You want to convert HTML email to plain text email.
  4. 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.
  5. 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 = "<h3>Quick, to the Batpoles!</h3>";
$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

By default, HTML To Markdown preserves HTML tags without Markdown equivalents, like <span> and <div>.

To strip HTML tags that don't have a Markdown equivalent while preserving the content inside them, set strip_tags to true, like this:

$converter = new HtmlConverter(array('strip_tags' => true));

$html = '<span>Turnips!</span>';
$markdown = $converter->convert($html); // $markdown now contains "Turnips!"

Or more explicitly, like this:

$converter = new HtmlConverter();
$converter->getConfig()->setOption('strip_tags', true);

$html = '<span>Turnips!</span>';
$markdown = $converter->convert($html); // $markdown now contains "Turnips!"

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 remove_nodes, like this:

$converter = new HtmlConverter(array('remove_nodes' => 'span div'));

$html = '<span>Turnips!</span><div>Monkeys!</div>';
$markdown = $converter->convert($html); // $markdown now contains ""

By default, all comments are stripped from the content. To preserve them, use the preserve_comments option, like this:

$converter = new HtmlConverter(array('preserve_comments' => true));

$html = '<span>Turnips!</span><!-- Monkeys! -->';
$markdown = $converter->convert($html); // $markdown now contains "Turnips!<!-- Monkeys! -->"

To preserve only specific comments, set preserve_comments with an array of strings, like this:

$converter = new HtmlConverter(array('preserve_comments' => array('Eggs!')));

$html = '<span>Turnips!</span><!-- Monkeys! --><!-- Eggs! -->';
$markdown = $converter->convert($html); // $markdown now contains "Turnips!<!-- Eggs! -->"

By default, placeholder links are preserved. To strip the placeholder links, use the strip_placeholder_links option, like this:

$converter = new HtmlConverter(array('strip_placeholder_links' => true));

$html = '<a>Github</a>';
$markdown = $converter->convert($html); // $markdown now contains "Github"

Style options

By default bold tags are converted using the asterisk syntax, and italic tags are converted using the underlined syntax. Change these by using the bold_style and italic_style options.

$converter = new HtmlConverter();
$converter->getConfig()->setOption('italic_style', '*');
$converter->getConfig()->setOption('bold_style', '__');

$html = '<em>Italic</em> and a <strong>bold</strong>';
$markdown = $converter->convert($html); // $markdown now contains "*Italic* and a __bold__"

Line break options

By default, 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 = '<p>test<br>line break</p>';

$converter->getConfig()->setOption('hard_break', true);
$markdown = $converter->convert($html); // $markdown now contains "test\nline break"

$converter->getConfig()->setOption('hard_break', false); // default
$markdown = $converter->convert($html); // $markdown now contains "test  \nline break"

Autolinking options

By default, a tags are converted to the easiest possible link syntax, i.e. if no text or title is available, then the <url> 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 = '<p><a href="https://thephpleague.com">https://thephpleague.com</a></p>';

$converter->getConfig()->setOption('use_autolinks', true);
$markdown = $converter->convert($html); // $markdown now contains "<https://thephpleague.com>"

$converter->getConfig()->setOption('use_autolinks', false); // default
$markdown = $converter->convert($html); // $markdown now contains "[https://google.com](https://google.com)"

Passing custom Environment object

You can pass current 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 = '<h3>Header</h3>
<img src="" />
';
$markdown = $converter->convert($html); // $markdown now contains "### Header" and "<img src="" />"

Table support

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:

use League\HTMLToMarkdown\HtmlConverter;
use League\HTMLToMarkdown\Converter\TableConverter;

$converter = new HtmlConverter();
$converter->getEnvironment()->addConverter(new TableConverter());

$html = "<table><tr><th>A</th></tr><tr><td>a</td></tr></table>";
$markdown = $converter->convert($html);

Limitations

  • Markdown Extra, MultiMarkdown and other variants aren't supported – just Markdown.

Style notes

  • 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 header_style to 'atx' in the options array when you instantiate the object:

    $converter = new HtmlConverter(array('header_style'=>'atx'));

    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.

Dependencies

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.

Contributors

Many thanks to all contributors so far. Further improvements and feature suggestions are very welcome.

How it works

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.

To-do

  • Support for nested lists and lists inside blockquotes.
  • Offer an option to preserve tags as HTML if they contain attributes that can't be represented with Markdown (e.g. style).

Trying to convert Markdown to HTML?

Use one of these great libraries:

No guarantees about the Elvish, though.

You might also like...
A highly configurable markdown renderer and Blade component for Laravel
A highly configurable markdown renderer and Blade component for Laravel

A highly configurable markdown renderer and Blade component for Laravel This package contains: a Blade component that can render markdown a highly con

Easily add routes to your Laravel app by creating Markdown or Blade files

Laravel Pages This package lets you create pages using Markdown or Blade without having to worry about creating routes or controllers yourself. Essent

Render colored  Markdown contents on console terminal
Render colored Markdown contents on console terminal

cli-markdown Render colored markdown contents on console terminal Preview run demo by php example/demo.php Features support auto render color on termi

markdown wiki/blog
markdown wiki/blog

Kwiki markdown wiki/blog Usage Place your markdown files in the /wiki directory. Categories are directories and subcategories are subdirectories. If y

Symfony 5 bundle to easily create dynamic subpages with Markdown. Useful for help sections and wikis.

MarkdownWikiBundle This bundle allows you to create rich subpages in a Symfony project using Markdown. Pages are stored in a file cache and sourced fr

Gruik ! An open-source markdown note-taking web app. [ABANDONED PROJECT]

What is Gruik ? It's a free & open-source note-taking service. A space where you can store notes, tutorials, code snippets... by writing them in markd

Docbook Tool for static documentation generation from Markdown files

Roave Docbook Tool Static HTML and PDF generator tool for generating documentation from Markdown files. Generates a deployable HTML file from Markdown

PHP Documentation system.

PHP Documentation system Simple but powerful Markdown docs. Features Search within Markdown files Customizable Twig templates (Note: default design is

Parser for Markdown and Markdown Extra derived from the original Markdown.pl by John Gruber.

PHP Markdown PHP Markdown Lib 1.9.0 - 1 Dec 2019 by Michel Fortin https://michelf.ca/ based on Markdown by John Gruber https://daringfireball.net/ Int

Convert HTML to Markdown with PHP

HTML To Markdown for PHP Library which converts HTML to Markdown for your sanity and convenience. Requires: PHP 7.2+ Lead Developer: @colinodell Origi

Convert HTML to Markdown with PHP

HTML To Markdown for PHP Library which converts HTML to Markdown for your sanity and convenience. Requires: PHP 7.2+ Lead Developer: @colinodell Origi

A package that uses blade templates to control how markdown is converted to HTML inside Laravel, as well as providing support for markdown files to Laravel views.
A package that uses blade templates to control how markdown is converted to HTML inside Laravel, as well as providing support for markdown files to Laravel views.

Install Install via composer. $ composer require olliecodes/laravel-etched-blade Once installed you'll want to publish the config. $ php artisan vendo

CssToInlineStyles is a class that enables you to convert HTML-pages/files into HTML-pages/files with inline styles. This is very usefull when you're sending emails.

CssToInlineStyles class Installation CssToInlineStyles is a class that enables you to convert HTML-pages/files into HTML-pages/files with inline style

A PHP tool that helps you write eBooks in markdown and convert to PDF.
A PHP tool that helps you write eBooks in markdown and convert to PDF.

Artwork by Eric L. Barnes and Caneco from Laravel News ❤️ . This PHP tool helps you write eBooks in markdown. Run ibis build and an eBook will be gene

A PHP component to convert HTML into a plain text format

html2text html2text is a very simple script that uses DOM methods to convert HTML into a format similar to what would be rendered by a browser - perfe

Simplexcel.php - Easily read / parse / convert / write between Microsoft Excel XML / CSV / TSV / HTML / JSON / etc spreadsheet tabular file formats

Simple Excel Easily parse / convert / write between Microsoft Excel XML / CSV / TSV / HTML / JSON / etc formats For further deatails see the GitHuib P

A PHP/Laravel package to fetch Notion Pages and convert it to HTML!

Generate HTML from Notion Page This package converts all the blocks in a Notion page into HTML using Notion's API. For more details on Notion API, ple

Generate pseudo-static pages from markdown and HTML files for Flarum

Flarum Pages Generator This is not a Flarum extension. This package provides a Flarum extender that you can use in the local extend.php to define cust

Convert HTML to PDF using Webkit (QtWebKit)

wkhtmltopdf and wkhtmltoimage wkhtmltopdf and wkhtmltoimage are command line tools to render HTML into PDF and various image formats using the QT Webk

Comments
  • Escape setext heading underlines

    Escape setext heading underlines

    Version(s) affected

    5.1.0

    Description

    According to https://spec.commonmark.org/0.30/#setext-heading-underline a line containing any number of =s makes the line above it a heading 1.

    How to reproduce

    HTML:

    <p>Foo<br>=<br>Bar</p>
    

    Output:

    Foo  
    =  
    Bar
    

    Expected output:

    Foo  
    \=  
    Bar
    
    bug commonmark-compatibility character-escaping 
    opened by olli7 0
  • Line breaks inside tag

    Line breaks inside tag

    Version(s) affected

    5.0.2

    Description

    Line breaks inside tags produce incorrect markdown

    How to reproduce

    HTML:

    <b>Hello<br><br>World</b>
    

    Output:

    **Hello  
      
    world**
    

    Expected output:

    **Hello**
      
    **world**
    
    bug up-for-grabs commonmark-compatibility 
    opened by multiwebinc 3
  •  <pre class=">

    
    	                                    
    	                                 

    Version(s) affected

    5.0.2

    Description

    How to reproduce

    html

    <pre class="language-"><code>GET /announcements
     </code></pre>
    

    after convert

    ```
    <pre class="language-">```
    GET /announcements
    
    ```
    ```
    
    bug commonmark-compatibility 
    opened by kbitlive 1
Releases(5.1.0)
  • 5.1.0(Mar 2, 2022)

  • 5.0.2(Nov 6, 2021)

  • 5.0.1(Sep 17, 2021)

  • 5.0.0(Mar 29, 2021)

    Added

    • Added support for tables (#203)
      • This feature is disable by default - see README for how to enable it
    • Added new strip_placeholder_links option to strip <a> tags without href attributes (#196)
    • Added new methods to ElementInterface:
      • hasParent()
      • getNextSibling()
      • getPreviousSibling()
      • getListItemLevel()
    • Added several parameter and return types across all classes
    • Added new PreConverterInterface to allow converters to perform any necessary pre-parsing

    Changed

    • Supported PHP versions increased to PHP 7.2 - 8.0
    • HtmlConverter::convert() may now throw a \RuntimeException when unexpected DOMDocument-related errors occur

    Fixed

    • Fixed complex nested lists containing heading and paragraphs (#198)
    • Fixed consecutive emphasis producing incorrect markdown (#202)
    Source code(tar.gz)
    Source code(zip)
  • 4.10.0(Jul 1, 2020)

  • 4.9.1(Dec 28, 2019)

  • 4.9.0(Nov 2, 2019)

  • 4.8.3(Oct 31, 2019)

  • 4.8.2(Aug 2, 2019)

    Fixed

    • Fixed headers not being placed onto a new line in some cases (#172)
    • Fixed handling of links containing spaces (#175)

    Removed

    • Removed support for HHVM
    Source code(tar.gz)
    Source code(zip)
  • 4.8.1(Dec 24, 2018)

    Added

    • Added support for PHP 7.3 :tada:

    Fixed

    • Fixed paragraphs following tables (#165, #166)
    • Fixed incorrect list item escaping (#168, #169)
    Source code(tar.gz)
    Source code(zip)
  • 4.8.0(Sep 18, 2018)

    Added

    • Added support for email auto-linking
    • Added a new interface (HtmlConverterInterface) for the main HtmlConverter class
    • Added additional test cases (#14)

    Changed

    • The italic_style option now defaults to '*' so that in-word emphasis is handled properly (#75)

    Fixed

    • Fixed several issues of <code> and <pre> tags not converting to blocks or inlines properly (#26, #70, #102, #140, #161, #162)
    • Fixed in-word emphasis using underscores as delimiter (#75)
    • Fixed character escaping inside of <div> elements
    • Fixed header edge cases

    Deprecated

    • The bold_style and italic_style options have been deprecated (#75)
    Source code(tar.gz)
    Source code(zip)
  • 4.7.0(May 19, 2018)

    Added

    • Added setOptions() function for chainable calling (#149)
    • Added new list_item_style_alternate option for converting every-other list with a different character (#155)

    Fixed

    • Fixed insufficient newlines after code blocks (#144, #148)
    • Fixed trailing spaces not being preserved in link anchors (#157)
    • Fixed list-like lines not being escaped inside of lists items (#159)
    Source code(tar.gz)
    Source code(zip)
  • 4.6.2(Jan 7, 2018)

  • 4.6.1(Jan 1, 2018)

  • 4.6.0(Oct 24, 2017)

  • 4.5.0(Oct 9, 2017)

  • 4.4.1(Mar 16, 2017)

  • 4.4.0(Dec 28, 2016)

    Added

    • Added hard_break configuration option (#112, #115)
    • The HtmlConverter can now be instantiated with an Environment (#118)

    Fixed

    • Fixed handling of paragraphs in list item elements (#47, #110)
    • Fixed phantom spaces when newlines follow br elements (#116, #117)
    • Fixed link converter not sanitizing inner spaces properly (#119, #120)
    Source code(tar.gz)
    Source code(zip)
  • 4.3.1(Oct 27, 2016)

    Changed

    • Revised the sanitization implementation (#109)

    Fixed

    • Fixed tag-like content not being escaped (#67, #109)
    • Fixed thematic break-like content not being escaped (#65, #109)
    • Fixed codefence-like content not being escaped (#64, #109)
    Source code(tar.gz)
    Source code(zip)
  • 4.3.0(Oct 26, 2016)

    Added

    • Added full support for PHP 7.0 and 7.1

    Changed

    • Changed <pre> and <pre><code> conversions to use backticks instead of indendation (#102)

    Fixed

    • Fixed issue where specified code language was not preserved (#70, #102)
    • Fixed issue where <code> tags nested in <pre> was not converted properly (#70, #102)
    • Fixed header-like content not being escaped (#76, #105)
    • Fixed blockquote-like content not being escaped (#77, #103)
    • Fixed ordered list-like content not being escaped (#73, #106)
    • Fixed unordered list-like content not being escaped (#71, #107)
    Source code(tar.gz)
    Source code(zip)
  • 4.2.2(Sep 27, 2016)

  • 4.2.1(May 18, 2016)

    Fixed

    • Fixed path to autoload.php when used as a library (#98)
    • Fixed edge case for tags containing only whitespace (#99)

    Removed

    • Removed double HTML entity decoding, as this is not desireable (#60)
    Source code(tar.gz)
    Source code(zip)
  • 4.2.0(Feb 1, 2016)

    Added

    • Added the ability to invoke HtmlConverter objects as functions (#85)

    Fixed

    • Fixed improper handling of nested list items (#19 and #84)
    • Fixed preceeding or trailing spaces within emphasis tags (#83)
    Source code(tar.gz)
    Source code(zip)
  • 4.1.1(Nov 20, 2015)

  • 4.1.0(Oct 29, 2015)

  • 4.0.1(Sep 1, 2015)

    Fixed

    • Added escaping to avoid * and _ in a text being rendered as emphasis (#48)

    Removed

    • Removed the demo (#51)
    • .styleci.yml and CONTRIBUTING.md are no longer included in distributions (#50)
    Source code(tar.gz)
    Source code(zip)
  • 4.0.0(Jul 25, 2015)

    This release changes the visibility of several methods/properties. #42 and #43 brought to light that some visiblities were not ideally set, so this releases fixes that. Moving forwards this should reduce the chance of introducing BC-breaking changes.

    Added

    • Added new HtmlConverter::getEnvironment() method to expose the Environment (#42, #43)

    Changed

    • Changed Environment::addConverter() from protected to public, enabling custom converters to be added (#42, #43)
    • Changed HtmlConverter::createDOMDocument() from protected to private
    • Changed Element::nextCached from protected to private
    • Made the Environment class final
    Source code(tar.gz)
    Source code(zip)
  • 3.1.1(Jul 23, 2015)

  • 3.1.0(Jul 20, 2015)

    Added

    • Added new equals method to Element to check for equality

    Changes

    • Use Linux line endings consistently instead of plaform-specific line endings (#36)

    Fixed

    • Cleaned up code style
    Source code(tar.gz)
    Source code(zip)
  • 3.0.0(Jul 14, 2015)

    Changed

    • Changed namespace to League\HTMLToMarkdown
    • Changed packagist name to league/html-to-markdown
    • Re-organized code into several separate classes
    • <a> tags with identical href and inner text are now rendered using angular bracket syntax (#31)
    • <div> elements are now treated as block-level elements (#33)
    Source code(tar.gz)
    Source code(zip)
Owner
The League of Extraordinary Packages
A group of developers who have banded together to build solid, well tested PHP packages using modern coding standards.
The League of Extraordinary Packages
Generate pseudo-static pages from markdown and HTML files for Flarum

Flarum Pages Generator This is not a Flarum extension. This package provides a Flarum extender that you can use in the local extend.php to define cust

Clark Winkelmann 7 Feb 21, 2022
Better Markdown Parser in PHP

Parsedown Better Markdown Parser in PHP - Demo. Features One File No Dependencies Super Fast Extensible GitHub flavored Tested in 5.3 to 7.3 Markdown

Emanuil Rusev 14.3k Dec 28, 2022
Highly-extensible PHP Markdown parser which fully supports the CommonMark and GFM specs.

league/commonmark league/commonmark is a highly-extensible PHP Markdown parser created by Colin O'Dell which supports the full CommonMark spec and Git

The League of Extraordinary Packages 2.4k Dec 29, 2022
A PHP tool to generate templateable markdown documentation from the docblocks or type-hints of your codebase.

Roster Installation To install, simply require the package using composer: composer require

Jordan LeDoux 14 Sep 25, 2022
PHP Markdown Engine Support

PHP Markdown Version v1.x support all PHP version >=5.4 v2.x support all PHP version >=7.0 Cài đặt thư viện Thư viện này được cài đặt thông qua Compos

Hung Nguyen 3 Jul 1, 2022
Rendering markdown from PHP code

JBZoo / Markdown Installing composer require jbzoo/markdown Usage Rendering Table <?php declare(strict_types=1); use JBZoo\Markdown\Table; echo (new

JBZoo Toolbox 1 Dec 26, 2021
PHP Markdown & Extra

PHP Markdown & Extra An updated and stripped version of the original PHP Markdown by Michel Fortin. Works quite well with PSR-0 autoloaders and is Com

dflydev 173 Dec 30, 2022
A simple regex-based Markdown parser in PHP

Slimdown A simple regex-based Markdown parser in PHP. Supports the following elements (and can be extended via Slimdown::add_rule()): Headers Links Bo

Aband*nthecar 16 Dec 24, 2022
A super lightweight Markdown parser for PHP projects and applications.

A speedy Markdown parser for PHP applications. This is a super lightweight Markdown parser for PHP projects and applications. It has a rather verbose

Ryan Chandler 15 Nov 8, 2022
PHP based Markdown documentation viewer

PHP based viewer for Markdown files, to view them with fenced code highlighting and navigation.

null 5 Dec 9, 2022