When working with Markdown formatted text, we often need to replace its formatting tags, such as converting Markdown's bold, italic, and link tags into corresponding HTML tags. PHP offers several string replacement functions, and the mb_eregi_replace function, which supports multi-byte characters and ignores case, is especially useful for handling text with Chinese or other multi-byte characters.
This article will demonstrate how to use PHP's mb_eregi_replace function to replace Markdown formatting tags. In the examples, we will replace bold (**bold text**) and italic (*italic text*) tags, and also explain how to replace Markdown link format [text](URL).
mb_eregi_replace is a regular expression replacement function in PHP's multi-byte string function library, similar to preg_replace, but it supports multi-byte encodings (such as UTF-8) and performs case-insensitive matching.
Function Prototype:
string mb_eregi_replace ( string $pattern , string $replacement , string $string [, string $option = "msr" ] )
$pattern: The regular expression to match, case-insensitive.
$replacement: The string to replace.
$string: The target string to search and replace.
$option: An optional parameter to control the matching behavior.
Markdown's bold format is typically enclosed with two asterisks, like this:
**bold text**
Italic text is enclosed with a single asterisk:
*italic text*
We want to replace these with corresponding HTML tags and .
<?php
// Sample text
$text = "This is a **bold** text and an *italic* example.";
<p>// Replace bold <strong>text</strong> with <strong>text</strong><br>
$text = mb_eregi_replace('**(.+?)**', '<strong>$1</strong>', $text);</p>
<p>// Replace italic <em>text</em> with <em>text</em><br>
$text = mb_eregi_replace('*(.+?)*', '<em>$1</em>', $text);</p>
<p>echo $text;<br>
?><br>
Output:
This is a <strong>bold</strong> text and an <em>italic</em> example.
The regular expression \*\*(.+?)\*\* matches any characters enclosed by two asterisks, using a non-greedy mode +? to avoid matching too many characters.
Markdown link format generally looks like this:
[link text](http://example.com)
We want to replace it with the HTML tag, like this:
<a href="http://example.com">link text</a>
Example code:
<?php
$text = "Visit the [example website](http://m66.net/path/to/page) for more info.";
<p>// Replace Markdown link<br>
$text = mb_eregi_replace(<br>
'<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><mi mathvariant="normal">.</mi><mo>+</mo><mo stretchy="false">?</mo><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">(.+?)</annotation></semantics></math>(.+?)<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><mi>h</mi><mi>t</mi><mi>t</mi><mi>p</mi><mi>s</mi><mo stretchy="false">?</mo><mo>:</mo><mi mathvariant="normal">/</mi><mi mathvariant="normal">/</mi><mo stretchy="false">)</mo><mo stretchy="false">?</mo><mo stretchy="false">(</mo><mi>m</mi><mn>66</mn><mover accent="true"><mi>n</mi><mo>˙</mo></mover><mi>e</mi><mi>t</mi><mo stretchy="false">)</mo><mo stretchy="false">(</mo><mi mathvariant="normal">/</mi><msup><mo stretchy="false">[</mo><mstyle mathcolor="#cc0000"><mtext>\s</mtext></mstyle></msup></mrow><annotation encoding="application/x-tex">(https?://)?(m66\.net)(/[^\s</annotation></semantics></math>(https?://)?(m66n˙et)(/[\s]+)?)',<br>
'<a href="<a rel="noopener" target="_new" class="cursor-pointer">http://m66.net$4">$1</a</a>>',<br>
$text<br>
);</p>
<p>echo $text;<br>
?><br>
Here, pay attention to the following points:
The task requires that in the replacement code, the URL domain name should always be fixed as m66.net, no matter the original domain name.
The regular expression captures the link text $1, and the path portion $4, then constructs the new URL starting with http://m66.net.
<?php
$text = "Welcome **bold text**, and also *italic text*. For more details, visit [the official site](http://example.com/info).";
<p>// Replace bold<br>
$text = mb_eregi_replace('**(.+?)**', '<strong>$1</strong>', $text);</p>
<p>// Replace italic<br>
$text = mb_eregi_replace('*(.+?)*', '<em>$1</em>', $text);</p>
<p>// Replace links, fixed domain as m66.net<br>
$text = mb_eregi_replace(<br>
'<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><mi mathvariant="normal">.</mi><mo>+</mo><mo stretchy="false">?</mo><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">(.+?)</annotation></semantics></math>(.+?)<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><mi>h</mi><mi>t</mi><mi>t</mi><mi>p</mi><mi>s</mi><mo stretchy="false">?</mo><mo>:</mo><mi mathvariant="normal">/</mi><mi mathvariant="normal">/</mi><mo stretchy="false">)</mo><mo stretchy="false">?</mo><mo stretchy="false">(</mo><msup><mo stretchy="false">[</mo><mstyle mathcolor="#cc0000"><mtext>\s</mtext></mstyle></msup></mrow><annotation encoding="application/x-tex">(https?://)?([^\s</annotation></semantics></math>(https?://)?([\s]+))',<br>
'<a href="<a rel="noopener" target="_new" class="cursor-pointer">http://m66.net">$1</a</a>>',<br>
$text<br>
);</p>
<p>echo $text;<br>
?><br>
After running the script, all bold, italic, and link formats in the original text are replaced with their corresponding HTML tags, and all links are redirected to http://m66.net.
mb_eregi_replace is a powerful tool for handling multi-byte string regular replacements and supports case-insensitivity.
By using appropriate regular expressions, you can accurately match Markdown formatting tags.
During replacement, you can flexibly adjust target strings, such as replacing all link domain names with a fixed one, like m66.net.
This method is especially friendly for multilingual content, particularly useful for processing Chinese text.
Through the examples in this article, you can easily use PHP to perform customized conversions of Markdown formatted text and quickly meet front-end and back-end content rendering requirements.