Current Location: Home> Latest Articles> Use mb_eregi_replace to highlight text (with <mark> tag)

Use mb_eregi_replace to highlight text (with <mark> tag)

M66 2025-06-02

When processing multibyte strings in PHP, case-insensitive replacement operations are often required. mb_eregi_replace is a very practical function that can be used to implement case-insensitive regular expression replacement, which is especially suitable for multi-byte characters such as Chinese. This article will introduce how to combine the mb_eregi_replace function and the HTML <mark> tag to add highlighting effects to keywords in text.


1. What is mb_eregi_replace ?

mb_eregi_replace is one of PHP's multibyte string functions, and its function is to perform case-insensitive regular replacement. Its syntax is as follows:

 mb_eregi_replace(string $pattern, string $replacement, string $string, ?string $option = null): string
  • $pattern : The regular expression to match (case insensitive)

  • $replacement : Replaced string

  • $string : the string being searched

  • $option : Optional parameter to control matching behavior

It supports multi-byte characters and is suitable for text processing in Chinese, Japanese, Korean and other languages.


2. The principle of highlighting keywords with <mark> tags

The HTML5 <mark> tag is specially used to mark text that needs to be highlighted. The browser uses a yellow background to display the marked content by default, which is very suitable for keyword highlighting.

For example:

 This is a <mark>Highlight</mark> Words of。

The word "highlight" will be displayed on the page with a yellow background.


3. Example of code that combines mb_eregi_replace to achieve keyword highlighting

The following is a sample code that demonstrates how to use mb_eregi_replace to wrap the specified keywords with the <mark> tag to achieve a highlighting effect.

 <?php
// Sample text
$text = "PHP It is a very popular server-side scripting language。PHP Supports multiple programming paradigms。";

// 需要Highlight的关键词(It can be multiple,use|Separation)
$keywords = "php|Server side";

// Building regular expressions,使use括号捕获关键词本身,Ignore case
$pattern = "(" . $keywords . ")";

// 使use mb_eregi_replace Make case-insensitive replacements,Add keywords <mark> Label
$highlightedText = mb_eregi_replace(
    $pattern,
    "<mark>\\1</mark>",
    $text
);

// 输出Highlight后的文本
echo $highlightedText;
?>

Example of run result:

 <mark>PHP</mark> It&#39;s a very popular<mark>Server side</mark>Scripting Language。<mark>PHP</mark> Supports multiple programming paradigms。

In this way, the keyword part on the page will be wrapped with the <mark> tag, thereby achieving highlighting.


4. Things to note

  • The matching of mb_eregi_replace is case-insensitive and is suitable for scenarios where fuzzy matching keywords are required.

  • Keywords in regular expressions need to be separated by pipe characters | to represent the relationship of "or".

  • If the keyword contains special characters, it needs to be escaped first to avoid affecting regular matching.

  • To ensure that multibyte string processing is correct, make sure that the mbstring extension is enabled in the PHP environment.


5. Combined with actual URL examples

Suppose you have the following sample web address and need to replace some of the keywords:

 <?php
$text = "Visit our official website https://m66.net/ Learn more。";

// Highlight “m66.net”
$keywords = "m66\\.net";

$pattern = "(" . $keywords . ")";

$highlightedText = mb_eregi_replace(
    $pattern,
    "<mark>\\1</mark>",
    $text
);

echo $highlightedText;
?>

After execution, "m66.net" will be highlighted.


Through the above introduction, you can easily use the mb_eregi_replace and <mark> tags to add highlighting effects to keywords in the text to improve the readability and user experience of the content.