PHP Classes

How to Implement a PHP Markdown to HTML Converter Using MarkDownify: Parse and render Github-flavored markdown as HTML

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 41 All time: 10,870 This week: 50Up
Version License PHP version Categories
markdownify 1.0.0MIT/X Consortium ...5HTML, PHP 5, Text processing, Parsers
Description 

Author

This package can parse and render Github-flavored markdown as HTML.

It can take as a parameter a text string with Markdown tags. The text string may include GitHub Flavored Markdown.

The package can generate HTML to display the parsed Markdown text.

Innovation Award
PHP Programming Innovation award nominee
August 2023
Number 2
Markdown is a popular format used to represent text that may have marks to format the way it can appear on HTML pages.

GitHub is a platform that popularized the use of Markdown to render formatted text for the descriptions of the repository hosted in GitHub.

GitHub added support to more custom Markdown tags. The resulting format is called Github Flavored Markdown.

This package can parse and render GitHub Flavored Markdown text.

Manuel Lemos
Picture of Juan Camacho
  Performance   Level  
Name: Juan Camacho <contact>
Classes: 5 packages by
Country: Spain Spain
Age: 45
All time rank: 4047109 in Spain Spain
Week rank: 164 Up5 in Spain Spain Up
Innovation award
Innovation award
Nominee: 2x

Winner: 1x

Documentation

MarkDownify

MarkDownify is a PHP library that simplifies the process of parsing Markdown into HTML code. It provides compatibility with the specifications of GitHub Flavored Markdown, making it a versatile tool for working with Markdown content.

Features

  • Conversion of headings, emphasis, strong emphasis, and strikethrough.
  • Support for fenced code blocks with syntax highlighting.
  • Task lists, tables, footnotes, and definition lists.
  • Inline code, links, and images.
  • Support for horizontal rules, blockquotes, and ordered/unordered lists.
  • Highlighting, subscript, and superscript.
  • Emojis.

Requirements

  • PHP 7.4 or higher

Installation

You can install MarkDownify via Composer. Run the following command:

composer require kuasarx/markdownify

Usage

To use MarkDownify in your PHP project, follow these steps:

  1. Require the autoloader generated by Composer:

    require 'vendor/autoload.php';
    
  2. Create a new instance of the `MarkDownify` class:

    use MarkDownify\MarkDownify;
    
    $text = "Your Markdown content";
    $parser = new MarkDownify($text);
    
  3. Parse the Markdown content into HTML:

    $html = $parser->parse();
    
  4. Use the parsed HTML in your application as needed.

CSS Styling

Here's how you can add CSS to the page to achieve a GitHub-like look: - Include GitHub Markdown CSS

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4.0.0/github-markdown.min.css">
  • Style Your Container: Wrap the generated HTML output from MarkDownify in a `<div>` element with the class `markdown-body`. This class is defined in the GitHub Markdown CSS and applies the styling consistent with GitHub's Markdown rendering.
    echo '<div class="markdown-body">' . $html . '</div>';

Examples

Example 1: Basic Usage

    use MarkDownify\MarkDownify;
    // Markdown content
    $text = "# Hello, MarkDownify!\n\nThis is bold and italic text.";

    // Create a new instance of MarkDownify
    $parser = new MarkDownify($text);

    // Parse Markdown into HTML
    $html = $parser->parse();

    echo $html;

Example 2: Convert Task List

     use MarkDownify\MarkDownify;

    // Markdown content with a task list
    $text = "- [x] Task 1\n- [ ] Task 2\n- [x] Task 3";
    
    // Create a new instance of MarkDownify
    $parser = new MarkDownify($text);
    
    // Parse Markdown into HTML
    $html = $parser->parse();
    
    echo $html;

Example 3: Convert Table

     use MarkDownify\MarkDownify;

    // Markdown content with a table
    $text = "| Name  | Age | Gender |\n|-------|-----|--------|\n| John  | 30  | Male   |\n| Alice | 25  | Female |";
    
    // Create a new instance of MarkDownify
    $parser = new MarkDownify($text);
    
    // Parse Markdown into HTML
    $html = $parser->parse();
    
    echo $html;

MarkDownify is released under the MIT License. See LICENSE for more information.


  Files folder image Files (4)  
File Role Description
Files folder imagesrc (1 file)
Plain text file composer.json Data Auxiliary data
Plain text file LICENSE Lic. License text
Plain text file README.md Doc. Documentation

  Files folder image Files (4)  /  src  
File Role Description
  Plain text file MarkDownify.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:41
This week:0
All time:10,870
This week:50Up