PHP code example of php-markdown / parser

1. Go to this page and download the library: Download php-markdown/parser library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

php-markdown / parser example snippets





declare(strict_types=1);
r;

$parser = new MarkdownParser();
$html   = $parser->parse('# Hello **World**');

echo $html; // <h1>Hello <strong>World</strong></h1>

$markdown = <<<MD
---
title: My Post
published: true
tags: php
---

# My Post

Content here.
MD;

['html' => $html, 'meta' => $meta] = $parser->parseWithMeta($markdown);

echo $meta['title'];     // My Post
echo $meta['published']; // true (bool)
echo $html;              // <h1>My Post</h1>\n<p>Content here.</p>\n

$markdown = <<<MD
| Name  | Score |
|:------|------:|
| Alice |    95 |
| Bob   |    87 |
MD;

echo $parser->parse($markdown);
// <table>
// <thead>
// <tr>
// <th align="left">Name</th>
// <th align="right">Score</th>
// </tr>
// </thead>
// <tbody>
// <tr>
// <td align="left">Alice</td>
// ...

$html = $parser->parse('<div class="note">text</div>', allowRawHtml: true);
// <div class="note">text</div>  — dangerous tags/attrs stripped by HtmlSanitizer

$parser = new MarkdownParser(
    maxBytes: 1_048_576,   // default; larger input throws ParseException (checked before and after NFC normalization)
    linkTarget: '_blank',  // adds target="…" to every rendered link and autolink
    linkRel: 'nofollow',   // adds rel="…"; "noopener" is appended automatically when target is _blank
);

echo $parser->parse('[a](/x)');
// <p><a href="/x" target="_blank" rel="nofollow noopener">a</a></p>
bash
composer 
bash
# PHPUnit (ll
php vendor/bin/phpunit --no-coverage

# Standalone (no PHPUnit needed)
php test.php