Markdown is a simple way to format text using plain characters. Instead of writing HTML tags manually, you can use symbols to create headings, bold text, links, images, lists, and more.
Inphinit PHP provides an experimental Markdown feature that allows you to convert Markdown content into HTML. This is especially useful when building blogs, documentation pages, content management systems, or any application that needs to display formatted text.
In this tutorial, we will explore the main features of the Inphinit Markdown parser with simple explanations and practical examples.
1. Converting a Markdown String into HTML
The easiest way to get started is by converting a Markdown string into HTML.
First, import the Markdown class and create a new parser instance.
use Inphinit\Experimental\Utility\Markdown; $parser = new Markdown(); $input = "# Welcome This is a **simple example** of Markdown. You can also write *italic text*."; $output = $parser->fromString($input); echo $output; How does it work?
-
Markdown: Imports the Markdown parser class. -
new Markdown(): Creates a new parser instance. -
fromString(): Converts the Markdown text into HTML. -
echo: Displays the generated HTML.
The result will be similar to:
<h1>Welcome</h1> <p>This is a <strong>simple example</strong> of Markdown.</p> <p>You can also write <em>italic text</em>.</p> Notice that you don't need to write HTML tags manually. The parser handles the conversion for you.
2. Understanding Basic Markdown Syntax
Before exploring more advanced features, let's look at some common Markdown syntax supported by the parser.
Headings
Use the # character to create headings. The number of characters determines the heading level.
# Main Heading ## Section Title ### Subsection Title This generates HTML headings from <h1> to <h3>.
For example, you can use headings to organize a blog post into sections.
Bold and Italic Text
Use two asterisks (**) to make text bold and one asterisk (*) to make it italic.
This is **important information**. This is *a simple example* of italic text. The generated HTML uses <strong> and <em> elements.
Links
You can create clickable links using square brackets and parentheses.
Visit the [Example](https://example.com). The parser converts this into an HTML anchor element:
<a href="https://example.com/">Example</a> Images
Images use a similar syntax to links, but with an exclamation mark (!) at the beginning.
 The parser generates an image element and uses the provided description as its alternative text.
This is useful when creating articles with images without writing HTML manually.
3. Converting Markdown Without Automatic Paragraphs
Sometimes you may want to convert Markdown into HTML without automatically wrapping the content in paragraph tags.
For this situation, Inphinit provides the fromInlineString() method.
use Inphinit\Experimental\Utility\Markdown; $parser = new Markdown(); $input = "**Hello!** This is a *Markdown* example."; $output = $parser->fromInlineString($input); echo $output; The result is:
<strong>Hello!</strong> This is a <em>Markdown</em> example. What is the difference?
The main difference is how the parser handles paragraphs and line breaks:
-
fromString(): Converts line breaks into HTML paragraphs. -
fromInlineString(): Preserves line breaks without automatically creating paragraph elements.
Use fromString() for complete content, such as blog articles. Use fromInlineString() when you need formatted text inside an existing HTML element.
4. Converting Markdown Files
You don't always need to store Markdown content inside a PHP variable.
The fromFile() method allows you to read a Markdown file and convert its contents into HTML.
For example, imagine you have a file named article.md:
# My First Article Welcome to my website! This is an article written using **Markdown**. You can easily organize your content. Now, you can convert it using PHP:
use Inphinit\Experimental\Utility\Markdown; $parser = new Markdown(); $output = $parser->fromFile('/path/to/article.md'); echo $output; The parser reads the file, processes the Markdown syntax, and returns the generated HTML.
Using the Storage Directory
Inphinit also allows you to read files from its system storage directory.
$output = $parser->fromFile( INPHINIT_SYSTEM . '/storage/article.md' ); echo $output; This approach is useful when you want to keep your articles separate from your PHP application code.
For example, you could store blog posts, documentation pages, or other content in Markdown files.
5. Enabling HTML Inside Markdown
By default, the parser does not allow arbitrary HTML tags inside Markdown documents.
However, you can enable this behavior using the enableHtml() method.
use Inphinit\Experimental\Utility\Markdown; $parser = new Markdown(); $parser->enableHtml(true); $input = " # My Article <div align='center'> Welcome to my website! </div> <video src='video.webm'></video> "; $output = $parser->fromString($input); echo $output; Why is this useful?
Sometimes Markdown alone is not enough.
For example, you may want to include:
- Custom HTML containers.
- Embedded videos.
- Special elements.
- HTML structures that are not available through Markdown syntax.
Enabling HTML gives you more flexibility when creating content.
Important: Only enable raw HTML for trusted content. If users can submit Markdown themselves, allowing arbitrary HTML can introduce cross-site scripting (XSS) vulnerabilities. Sanitize untrusted HTML before displaying it.
6. Using Custom Markdown Syntax
One interesting feature of the Inphinit parser is its support for custom syntax.
By default, the constructor enables additional formatting rules.
These rules allow you to create HTML elements using simple delimiters.
Highlighted Text
Use !! around a word or sentence to generate a <mark> element.
This is !!important information!!. Generated HTML:
<p>This is <mark>important information</mark>.</p> This is useful when you want to highlight important information in an article.
Variables
Use %% to represent variables.
The default value is %%username%%. Generated HTML:
<p>The default value is <var>username</var>.</p> This is useful for documenting variables or displaying variable names in technical documentation.
Inserted Text
Use ++ to represent inserted content.
The new price is ++$29.99++. Generated HTML:
<p>The new price is <ins>$29.99</ins>.</p> This can be useful when showing changes to text or documents.
Deleted Text
Use -- to represent deleted content.
The old price was --$39.99--. Generated HTML:
<p>The old price was <del>$39.99</del>.</p> This is particularly useful when showing corrections, revisions, or removed information.
Disabling Custom Syntax
If you don't want these additional formatting rules, you can disable them when creating the parser.
$parser = new Markdown(false); The constructor accepts a boolean parameter named $enableCustom. Its default value is true.
7. Creating Your Own Inline Syntax
You can also define your own Markdown-like syntax using setCustomInline().
For example, imagine you want to create a syntax that centers text.
You could use @@ as the delimiter:
use Inphinit\Experimental\Utility\Markdown; $parser = new Markdown(); $parser->setCustomInline( '@@', '<div align="center">{contents}</div>' ); $input = 'Welcome to @@my website@@!'; $output = $parser->fromString($input); echo $output; The custom syntax transforms the content between the delimiters into the HTML template.
The generated HTML will be similar to:
<p>Welcome to <div align="center">my website</div>!</p> The {contents} placeholder represents the text placed between the delimiters.
This feature is useful when you want to extend Markdown with formatting rules that are specific to your application.
8. Customizing the Generated HTML
Inphinit also allows you to customize the HTML generated by the parser.
You can use the setTemplate() method to change the default HTML templates.
For example, imagine you want all generated images to load lazily.
You can customize the image template like this:
use Inphinit\Experimental\Utility\Markdown; $parser = new Markdown(); $parser->setTemplate( Markdown::FIGURE, '<img src="{url}" alt="{alternative}" loading="lazy">' ); $input = ''; $output = $parser->fromString($input); echo $output; The generated image will include the loading="lazy" attribute.
Other Available Templates
The parser provides several predefined constants that you can customize.
| Constant | Purpose |
|---|---|
Markdown::BLOCKQUOTE | Blockquotes |
Markdown::CODE_BLOCK | Code blocks |
Markdown::H1 | Heading level 1 |
Markdown::H2 | Heading level 2 |
Markdown::H3 | Heading level 3 |
Markdown::H4 | Heading level 4 |
Markdown::H5 | Heading level 5 |
Markdown::H6 | Heading level 6 |
Markdown::HR | Horizontal rules |
Markdown::ANCHOR | Links |
Markdown::FIGURE | Images |
Markdown::UL | Unordered lists |
Markdown::OL | Ordered lists |
Markdown::BOLD | Bold text |
Markdown::CODE | Inline code |
Markdown::ITALIC | Italic text |
Markdown::STRIKETHROUGH | Strikethrough |
Markdown::TABLE | Tables |
Markdown::SUBSCRIPT | Subscript |
Markdown::SUPERSCRIPT | Superscript |
For example, you can customize how headings are generated:
$parser->setTemplate( Markdown::H1, '<h1 class="page-title">{contents}</h1>' ); Now, Markdown headings using # will generate an HTML <h1> element with the page-title CSS class.
This is useful when you want to integrate Markdown content with your website's existing design.
9. Handling Parser Errors
When working with user-generated content or large Markdown files, you may encounter invalid syntax.
Inphinit provides the enableErrors() method to control how the parser handles serious errors.
By default, errors are disabled:
$parser = new Markdown(); $parser->enableErrors(false); When errors are disabled, faulty syntax is generally treated as regular text.
If you want the parser to throw exceptions when serious errors occur, enable error reporting:
$parser->enableErrors(true); try { $output = $parser->fromString($input); echo $output; } catch (\Throwable $error) { echo 'Markdown parsing failed: ' . $error->getMessage(); } This can help during development because it makes serious parsing problems easier to identify.
10. A Practical Example: Building a Simple Blog Article
Now that we have explored the main features, let's put them together in a practical example.
Imagine you want to create a simple blog system where articles are written in Markdown files.
First, create an article file named welcome.md:
# Welcome to My Blog This is my **first article** using Inphinit Markdown. ## About This Blog Here, I share tutorials, programming tips, and useful information. ## My Favorite Topics - PHP development - Web technologies - Open-source projects ## Conclusion Thank you for reading! Visit the [Inphinit documentation](https://inphinit.github.io/en/docs/). Next, create a PHP script to read and convert the article:
use Inphinit\Experimental\Utility\Markdown; $parser = new Markdown(); $article = $parser->fromFile( INPHINIT_SYSTEM . '/storage/welcome.md' ); echo $article; That's it!
Your Markdown file is now converted into HTML and can be displayed as part of your blog.
You can also customize the HTML templates to match your website's design.
Conclusion
The Inphinit Markdown parser provides a straightforward way to work with Markdown in PHP applications.
With just a few methods, you can convert strings and files into HTML, customize generated elements, enable HTML support, and even create your own formatting syntax.
Here are the main methods to remember:
-
fromString(): Converts Markdown strings into HTML with paragraphs. -
fromInlineString(): Converts Markdown without automatically creating paragraphs. -
fromFile(): Reads and converts a Markdown file. -
enableHtml(): Controls whether HTML tags are allowed. -
enableErrors(): Controls how serious parsing errors are handled. -
setCustomInline(): Defines custom inline syntax. -
setTemplate(): Customizes the generated HTML.
Since this is an experimental feature, its API and behavior may change in future versions of Inphinit.
If you are building a blog, documentation system, or content-driven application, Markdown can make content creation much simpler while allowing you to keep control over the final HTML output.
