
$converter-> getConfig()-> setOption( 'hard_break', true) Set hard_break to true to omit the two spaces, as per GitHub Flavored Markdown (GFM). $markdown = $converter-> convert( $html) // $markdown now contains "*Italic* and a _bold_" Line break optionsīy default, br tags are converted to two spaces followed by a newline character as per traditional Markdown. $converter-> getConfig()-> setOption( 'bold_style', '_') $converter-> getConfig()-> setOption( 'italic_style', '*') Change these by using the bold_style and italic_style options. $markdown = $converter-> convert( $html) // $markdown now contains "Github" Style optionsīy default bold tags are converted using the asterisk syntax, and italic tags are converted using the underlined syntax. To strip the placeholder links, use the strip_placeholder_links option, like this: $converter = new HtmlConverter( array( 'strip_placeholder_links' => true)) To preserve only specific comments, set preserve_comments with an array of strings, like this: $converter = new HtmlConverter( array( 'preserve_comments' => array( 'Eggs!'))) īy default, placeholder links are preserved. To preserve them, use the preserve_comments option, like this: $converter = new HtmlConverter( array( 'preserve_comments' => true)) $markdown = $converter-> convert( $html) // $markdown now contains ""īy default, all comments are stripped from the content. 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'))

Note that only the tags themselves are stripped, not the content they hold. $converter-> getConfig()-> setOption( 'strip_tags', true)

Or more explicitly, like this: $converter = new HtmlConverter() $markdown = $converter-> convert( $html) // $markdown now contains "Turnips!"

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)) Conversion optionsīy default, HTML To Markdown preserves HTML tags without Markdown equivalents, like and. The included demo directory contains an HTML->Markdown conversion form to try out. The $markdown variable now contains the Markdown version of your HTML as a string: echo $markdown // => # Quick, to the Batpoles! $markdown = $converter-> convert( $html) Next, create a new HtmlConverter instance, passing in your valid HTML code to its convert() function: use League\ HTMLToMarkdown\ HtmlConverter Require the library by issuing this command: composer require league/html-to-markdownĪdd require 'vendor/autoload.php' to the top of your script. You'd quite like to be able to speak Elvish.

