Skip to content

Guide · Open source

How to use Parsedown: install and render Markdown in PHP

Updated · written with AI from the project's own documentation and checked against our catalogue data

Parsedown is a PHP library that converts Markdown text into HTML. It is aimed at PHP developers who want to show formatted text, such as user comments or content pages, without writing HTML by hand. This guide shows you how to install it, convert text, and turn on safe mode for user-written content.

Parsedownerusev/parsedown · ★ 15k · MIT

Quick start

  1. 1Check your PHP version. Confirm you are running PHP 7.1 or newer, the prerequisite listed in the README.
  2. 2Install the package. Run composer require erusev/parsedown in your project directory.
  3. 3Create a Parsedown object. In your PHP file, instantiate the Parsedown class with new Parsedown().
  4. 4Convert Markdown. Pass your Markdown string to text() and echo the returned HTML.
  5. 5Enable safe mode for user input. Call setSafeMode(true) before parsing any user-written content.
  6. 6Test your output. Check the rendered HTML on your page and review any extensions you use for safety.

What Parsedown does

Markdown is a simple plain-text format for headings, lists, links and emphasis. Parsedown reads Markdown text and returns the equivalent HTML, which your PHP application can then output on a page.

The library is a single file with no dependencies. The README lists it as GitHub flavored, tested in PHP 7.1+, and extensible.

  • One file
  • No dependencies
  • Extensible
  • GitHub flavored
  • Markdown Extra extension available as a separate project

Install Parsedown with Composer

The README recommends installing the Composer package. You need PHP 7.1 or newer. Run the following command in your project directory.

If you do not use Composer, the README says you can instead download the latest release from the GitHub releases page and include Parsedown.php in your code.

  • Prerequisite: PHP 7.1+
  • Alternative: download the latest release and include Parsedown.php
composer require erusev/parsedown

Convert Markdown to HTML with text()

Create a Parsedown object and pass your Markdown to the text() method. It returns an HTML string. The README's example converts an emphasised word into an em element inside a paragraph.

Use line() when you only want inline parsing, for example a short label that should not be wrapped in a paragraph tag.

$Parsedown = new Parsedown(); echo $Parsedown->text('Hello _Parsedown_!'); # prints: <p>Hello <em>Parsedown</em>!</p> echo $Parsedown->line('Hello _Parsedown_!'); # prints: Hello <em>Parsedown</em>!

Turn on safe mode for user-written Markdown

Parsedown can escape user input within the HTML it generates and sanitises scripting vectors such as unsafe link destinations. The README says to call setSafeMode(true) when you are processing untrusted user input.

The README also warns that safe mode does not necessarily make extensions safe. Evaluate each extension yourself. For defence in depth, the README suggests a Content-Security-Policy. If you want to allow HTML inside user input, it recommends an HTML sanitiser such as HTML Purifier that whitelists tags.

  • Use setSafeMode(true) for untrusted input
  • Evaluate extensions separately for XSS safety
  • Consider a Content-Security-Policy as an extra layer
  • Use a whitelist sanitiser such as HTML Purifier if you allow HTML
$Parsedown->setSafeMode(true);

Avoid setMarkupEscaped for untrusted input

The README includes a method for escaping HTML in trusted input. It carries a warning that it is not safe from XSS. It still allows users to insert unsafe scripting vectors, such as a link with a javascript: destination.

Use this only when the input is trusted. For user-written content, use safe mode instead.

$Parsedown->setMarkupEscaped(true);

Know the limits before you adopt it

The README says Parsedown passes most CommonMark tests, but not all. The tests it fails mostly cover uncommon cases. If your project needs full CommonMark compliance, check a CommonMark implementation instead.

The library is for PHP only. It is code to integrate, not a finished end-user application. The README also links to a wiki and a video tutorial for more examples.

  • Not for non-PHP projects
  • Not fully CommonMark compliant
  • Integration code, not an end-user app

What people use it for

  • Rendering user comments written in Markdown on a PHP website
  • Displaying content pages written in Markdown inside a PHP web application
  • Adding custom Markdown syntax through Parsedown's extensibility
  • Showing inline formatted labels with the line() method

Frequently asked questions

How do I install Parsedown?

Run composer require erusev/parsedown in your project directory. The README also allows you to download the latest release and include Parsedown.php manually.

What do I need to run Parsedown?

The README lists PHP 7.1 or newer as the prerequisite. The library has no external dependencies.

Is Parsedown free to use?

Parsedown is released under the MIT licence, as listed on its project page. Check the licence file in the repository for the full terms.

Is Parsedown safe for user comments?

The README says to enable setSafeMode(true) for untrusted user input. It also advises caution with extensions and suggests a Content-Security-Policy as an extra layer.

Is Parsedown fully CommonMark compliant?

No. The README says it passes most CommonMark tests, but not all. The failing cases are described as quite uncommon.

How does Parsedown compare to Parsedown Extra or CommonMark PHP?

Parsedown Extra is an extension that adds Markdown Extra syntax, according to the README link. CommonMark PHP (league/commonmark) is another option if you need stricter CommonMark compliance; this guide does not benchmark them.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

Parsedown suits PHP developers who need a small, dependency-free Markdown parser inside a PHP web application, and who will enable safe mode for user content. Pick another tool if you need full CommonMark compliance, use a language other than PHP, or want a finished end-user app.

Install options & downloads