SnapShooter Backups Server, Database, Application and Laravel Backups - Get fully protected with SnapShooter

Markdown Processing in PHP

Markdown is a text-to-HTML conversion tool for web writers. It has gained its deserved popularity after its introduction on 2004. Markdown allows you to write text while providing a format in an easy and clean way.

Though it is developed by Perl, it has been ported to different languages such as PHP. In this tutorial, we will show you how to parse markdown in PHP. There is already a library league/commonmark developed by PHP League and it supports full CommonMark spec. We will be using this package across the tutorial.

Processing headers

First thing first, let's install league/commonmark using Composer as usual.

composer require league/commonmark

In the first example, we need to parse file headers.md which contains content as shown below:

# This is an H1

## This is an H2

###### This is an H6

We will first include the autoloader from Composer vendor folder. Then we will import CommonMarkConverter class and instantiate an instance of it. At last, we use the method convertToHtml to parse the content of headers.md.

include 'vendor/autoload.php';
 
use League\CommonMark\CommonMarkConverter;
 
$converter = new CommonMarkConverter();
 
echo $converter->convertToHtml(file_get_contents('headers.md'));

The outcome of running the script above is as shown below:

<h1>This is an H1</h1>
<h2>This is an H2</h2>
<h6>This is an H6</h6>

Processing lists

In this example, we need to parse file lists.md which contains content as shown below:

* This is a
* This is b
* This is c

This is A
This is B
This is C

Similar to the previous example, we use the same method convertToHtml to do the job for us.

include 'vendor/autoload.php';
 
use League\CommonMark\CommonMarkConverter;
 
$converter = new CommonMarkConverter();
 
echo $converter->convertToHtml(file_get_contents('lists.md'));

The outcome of running the script above is as shown below:

<ul>
<li>This is a</li>
<li>This is b</li>
<li>This is c</li>
</ul>
<ul>
<li>This is A</li>
<li>This is B</li>
<li>This is C</li>
</ul>

In this example, we need to parse file link.md which contains content as shown below:

[this is a link](www.google.com)

Similar to previous example, we use the same method convertToHtml to do the job for us.

We will again use the same method:

include 'vendor/autoload.php';
 
use League\CommonMark\CommonMarkConverter;
 
$converter = new CommonMarkConverter();
 
echo $converter->convertToHtml(file_get_contents('link.md'));

The outcome of running the script above is as shown below:

<p><a href="www.google.com">this is a link</a></p>

Processing table

In this example, we need to parse file table.md which contains content as shown below:

th | th(center) | th(right)
---|:----------:|----------:
td | td         | td

CommonMark is not able to parse table out of box, we need to install an extension to make that possible.

composer require webuni/commonmark-table-extension

We will create an environment, then add the table extension to the environment. Then create a converter using the environment. At last, we parse the markdown file as normal.

include 'vendor/autoload.php';
 
use League\CommonMark\Converter;
use League\CommonMark\DocParser;
use League\CommonMark\Environment;
use League\CommonMark\HtmlRenderer;
use Webuni\CommonMark\TableExtension\TableExtension;
 
$environment = Environment::createCommonMarkEnvironment();
 
$environment->addExtension(new TableExtension());
 
$converter = new Converter(new DocParser($environment), new HtmlRenderer($environment));
 
echo $converter->convertToHtml(file_get_contents('table.md'));

The outcome of running the script above is as shown below:

<table>
<thead>
<tr>
<th>th</th>
<th align="center">th(center)</th>
<th align="right">th(right)</th>
</tr>
</thead>
<tbody>
<tr>
<td>td</td>
<td align="center">td</td>
<td align="right">td</td>
</tr>
</tbody>
</table>

The end

Hopefully this simple tutorial helped you with your development. If you like our post, please follow us on Twitter and help spread the word. We need your support to continue. If you have questions or find our mistakes in above tutorial, do leave a comment below to let us know.