PHPackages                             nickcernis/html-to-markdown - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. nickcernis/html-to-markdown

Abandoned → [league/html-to-markdown](/?search=league%2Fhtml-to-markdown)Library[Parsing &amp; Serialization](/categories/parsing)

nickcernis/html-to-markdown
===========================

An HTML-to-markdown conversion helper for PHP

5.1.1(2y ago)1.9k211.0k↓24.5%215[9 PRs](https://github.com/thephpleague/html-to-markdown/pulls)5MITPHPPHP ^7.2.5 || ^8.0CI passing

Since Aug 14Pushed 3mo ago41 watchersCompare

[ Source](https://github.com/thephpleague/html-to-markdown)[ Packagist](https://packagist.org/packages/nickcernis/html-to-markdown)[ Docs](https://github.com/thephpleague/html-to-markdown)[ Fund](https://www.colinodell.com/sponsor)[ Fund](https://www.paypal.me/colinpodell/10.00)[ RSS](/packages/nickcernis-html-to-markdown/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (47)Used By (5)

HTML To Markdown for PHP
========================

[](#html-to-markdown-for-php)

[![Latest Version](https://camo.githubusercontent.com/830ce915897b69426d98ab6d33d5c4d31e1ed42ca1597cc0824525070751a4a4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c65616775652f68746d6c2d746f2d6d61726b646f776e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/league/html-to-markdown)[![Software License](https://camo.githubusercontent.com/30597ff9a350144f03bffdd9183e16468e0b3ca1193e1d08591d992622738d55/687474703a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Build Status](https://camo.githubusercontent.com/06d6b9f90e384876bc5a9b33efda1f8c3fa73db03cec598f7867eac1c4e5321e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f7468657068706c65616775652f68746d6c2d746f2d6d61726b646f776e2f54657374732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://github.com/thephpleague/html-to-markdown/actions?query=workflow%3ATests+branch%3Amaster)[![Coverage Status](https://camo.githubusercontent.com/5f57d1e6aa60abaf333d053de851a4d67ae43f9109f4f30aa8797e921a57e94b/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f7468657068706c65616775652f68746d6c2d746f2d6d61726b646f776e2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/thephpleague/html-to-markdown/code-structure)[![Quality Score](https://camo.githubusercontent.com/8e891bff96cf7f7d4d647417f85bec8dcf16a869cf5c73825cd1de894b60b81f/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f7468657068706c65616775652f68746d6c2d746f2d6d61726b646f776e2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/thephpleague/html-to-markdown)[![Total Downloads](https://camo.githubusercontent.com/ebebe53c73fbfd5715831c441f479e05163b41c0cadacaa8fadecb8c3e01d4e9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c65616775652f68746d6c2d746f2d6d61726b646f776e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/league/html-to-markdown)

Library which converts HTML to [Markdown](http://daringfireball.net/projects/markdown/) for your sanity and convenience.

**Requires**: PHP 7.2+

**Lead Developer**: [@colinodell](http://twitter.com/colinodell)

**Original Author**: [@nickcernis](http://twitter.com/nickcernis)

### Why convert HTML to Markdown?

[](#why-convert-html-to-markdown)

*"What alchemy is this?"* you mutter. *"I can see why you'd convert [Markdown to HTML](https://github.com/thephpleague/commonmark),"* 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

[](#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-&gt;Markdown conversion form to try out.

### Conversion options

[](#conversion-options)

Caution

By default, this library preserves HTML tags without Markdown equivalents, like ``, ``, ``, ``, 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](https://github.com/ezyang/htmlpurifier)) to provide additional HTML filtering.

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 = 'Turnips!';
$markdown = $converter->convert($html); // $markdown now contains "Turnips!"
```

Or more explicitly, like this:

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

$html = 'Turnips!';
$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 = 'Turnips!Monkeys!';
$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 = 'Turnips!';
$markdown = $converter->convert($html); // $markdown now contains "Turnips!"
```

To preserve only specific comments, set `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!"
```

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 = 'Github';
$markdown = $converter->convert($html); // $markdown now contains "Github"
```

### Style options

[](#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 = 'Italic and a bold';
$markdown = $converter->convert($html); // $markdown now contains "*Italic* and a __bold__"
```

### Line break options

[](#line-break-options)

By default, `br` tags are converted to two spaces followed by a newline character as per [traditional Markdown](https://daringfireball.net/projects/markdown/syntax#p). Set `hard_break` to `true` to omit the two spaces, as per GitHub Flavored Markdown (GFM).

```
$converter = new HtmlConverter();
$html = 'testline break';

$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

[](#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 `` 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 = 'https://thephpleague.com';

$converter->getConfig()->setOption('use_autolinks', true);
$markdown = $converter->convert($html); // $markdown now contains ""

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

### Passing custom Environment object

[](#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 = 'Header

';
$markdown = $converter->convert($html); // $markdown now contains "### Header" and ""
```

### Table support

[](#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 = "Aa";
$markdown = $converter->convert($html);
```

### Limitations

[](#limitations)

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

### Style notes

[](#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

[](#dependencies)

HTML To Markdown requires PHP's [xml](http://www.php.net/manual/en/xml.installation.php), [lib-xml](http://www.php.net/manual/en/libxml.installation.php), and [dom](http://www.php.net/manual/en/dom.installation.php) 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

[](#contributors)

Many thanks to all [contributors](https://github.com/thephpleague/html-to-markdown/graphs/contributors) so far. Further improvements and feature suggestions are very welcome.

### How it works

[](#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

[](#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?

[](#trying-to-convert-markdown-to-html)

Use one of these great libraries:

- [league/commonmark](https://github.com/thephpleague/commonmark) (recommended)
- [cebe/markdown](https://github.com/cebe/markdown)
- [PHP Markdown](https://michelf.ca/projects/php-markdown/)
- [Parsedown](https://github.com/erusev/parsedown)

No guarantees about the Elvish, though.

###  Health Score

62

—

FairBetter than 99% of packages

Maintenance54

Moderate activity, may be stable

Popularity60

Solid adoption and visibility

Community42

Growing community involvement

Maturity81

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 59.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~95 days

Recently: every ~209 days

Total

39

Last Release

1041d ago

Major Versions

2.2.2 → 3.0.02015-07-14

3.1.1 → 4.0.02015-07-25

4.10.0 → 5.0.02021-03-29

PHP version history (4 changes)2.0.0PHP &gt;=5.2

2.2.0PHP &gt;=5.3

3.0.0PHP &gt;=5.3.3

5.0.0PHP ^7.2.5 || ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/4325b62a6ad366c731c3120595d861469be50f9da88df3ea99752c30ff98c179?d=identicon)[colinodell](/maintainers/colinodell)

![](https://www.gravatar.com/avatar/e35e5f7ab928a5cbac804de6f32e936d6f49ed1c42464ea1d1842ef5e3960577?d=identicon)[nickcernis](/maintainers/nickcernis)

---

Top Contributors

[![colinodell](https://avatars.githubusercontent.com/u/202034?v=4)](https://github.com/colinodell "colinodell (226 commits)")[![nickcernis](https://avatars.githubusercontent.com/u/647669?v=4)](https://github.com/nickcernis "nickcernis (61 commits)")[![unlight](https://avatars.githubusercontent.com/u/301732?v=4)](https://github.com/unlight "unlight (18 commits)")[![andreskrey](https://avatars.githubusercontent.com/u/8786949?v=4)](https://github.com/andreskrey "andreskrey (17 commits)")[![schucan](https://avatars.githubusercontent.com/u/5184263?v=4)](https://github.com/schucan "schucan (7 commits)")[![Sylry](https://avatars.githubusercontent.com/u/9798412?v=4)](https://github.com/Sylry "Sylry (6 commits)")[![renovate[bot]](https://avatars.githubusercontent.com/in/2740?v=4)](https://github.com/renovate[bot] "renovate[bot] (6 commits)")[![WouterSioen](https://avatars.githubusercontent.com/u/1398405?v=4)](https://github.com/WouterSioen "WouterSioen (4 commits)")[![jdufresne](https://avatars.githubusercontent.com/u/347634?v=4)](https://github.com/jdufresne "jdufresne (4 commits)")[![localheinz](https://avatars.githubusercontent.com/u/605483?v=4)](https://github.com/localheinz "localheinz (4 commits)")[![steveh-bipsync](https://avatars.githubusercontent.com/u/152972172?v=4)](https://github.com/steveh-bipsync "steveh-bipsync (4 commits)")[![IllyaMoskvin](https://avatars.githubusercontent.com/u/3881840?v=4)](https://github.com/IllyaMoskvin "IllyaMoskvin (3 commits)")[![markopy](https://avatars.githubusercontent.com/u/48253511?v=4)](https://github.com/markopy "markopy (2 commits)")[![filipgolonka](https://avatars.githubusercontent.com/u/2419247?v=4)](https://github.com/filipgolonka "filipgolonka (2 commits)")[![bluehaoran](https://avatars.githubusercontent.com/u/3521663?v=4)](https://github.com/bluehaoran "bluehaoran (2 commits)")[![gitter-badger](https://avatars.githubusercontent.com/u/8518239?v=4)](https://github.com/gitter-badger "gitter-badger (1 commits)")[![nlisgo](https://avatars.githubusercontent.com/u/383397?v=4)](https://github.com/nlisgo "nlisgo (1 commits)")[![pdelre](https://avatars.githubusercontent.com/u/1379248?v=4)](https://github.com/pdelre "pdelre (1 commits)")[![johnsvenn](https://avatars.githubusercontent.com/u/684983?v=4)](https://github.com/johnsvenn "johnsvenn (1 commits)")[![sewid](https://avatars.githubusercontent.com/u/359766?v=4)](https://github.com/sewid "sewid (1 commits)")

---

Tags

commonmarkconverterhacktoberfesthtmlmarkdownphpphpleaguehtmlmarkdown

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Psalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/nickcernis-html-to-markdown/health.svg)

```
[![Health](https://phpackages.com/badges/nickcernis-html-to-markdown/health.svg)](https://phpackages.com/packages/nickcernis-html-to-markdown)
```

###  Alternatives

[league/html-to-markdown

An HTML-to-markdown conversion helper for PHP

1.9k28.6M199](/packages/league-html-to-markdown)[pixel418/markdownify

The HTML to Markdown converter for PHP

196800.8k8](/packages/pixel418-markdownify)[texy/texy

Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated against spambots), national typographic single and double quotation marks, ellipses, em dashes, dimension sign, nonbreakable spaces (e.g. in phone numbers), acronyms, arrows and many others. Texy code can optionally contain HTML tags.

161838.9k15](/packages/texy-texy)[interaction-design-foundation/nova-html-card

A Laravel Nova card to display arbitrary HTML content

67731.2k3](/packages/interaction-design-foundation-nova-html-card)[3f/converter

A set of classes to translate a text from HTML to BBcode and from BBCode to Markdown.

29148.7k](/packages/3f-converter)[jbroadway/slimdown

A simple regex-based Markdown parser.

357.7k](/packages/jbroadway-slimdown)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
