Current Location: Home> Latest Articles> How to Use the mb_eregi_replace Function with Arrays to Achieve Multi-pattern and Multi-rule Text Replacement?

How to Use the mb_eregi_replace Function with Arrays to Achieve Multi-pattern and Multi-rule Text Replacement?

M66 2025-06-23

In PHP text processing, the mb_eregi_replace() function, which supports multibyte strings and case-insensitive replacements, is particularly suitable for handling UTF-8 encoded text. Although this function has been deprecated in newer PHP versions (with recommendations to use mb_ereg_replace() or preg_replace()), it still has its place in certain projects or specific environments.

This article will introduce how to use the mb_eregi_replace() function in combination with arrays to implement one-to-many, many-to-one, or many-to-many text replacement strategies.

Basic Syntax

The basic syntax of mb_eregi_replace() is as follows:

mb_eregi_replace(string $pattern, string $replacement, string $string, string $option = "msr")
  • $pattern: The regular expression pattern

  • $replacement: The replacement content

  • $string: The target string

  • $option: Matching options (optional)

Application Scenario Analysis

Suppose we want to implement the following replacement logic:

  • Replace all links starting with "http://" or "https://" with [link]

  • Replace words like "测试" (test) and "范例" (example) with "示例" (sample)

  • Replace multiple keywords, such as "苹果" (Apple) → "Apple", "谷歌" (Google) → "Google"

We can use an array to combine these rules and use array_map() or a loop to apply the replacements sequentially.

Example Code

Here is a complete example demonstrating how to use an array with mb_eregi_replace() to achieve complex text replacements:

<?php
<p>// Original text<br>
$text = <<<TEXT<br>
This is a test text, containing links: <a rel="noopener" target="_new" class="" href="http://m66.net/page">http://m66.net/page</a> and <a rel="noopener" target="_new" class="" href="https://m66.net/example">https://m66.net/example</a>.<br>
Additionally, there are some brand names like 苹果 (Apple), 谷歌 (Google), and other example content.<br>
TEXT;</p>
<p>// Replacement rules array: keys are patterns, values are replacement content<br>
$replace_rules = [<br>
// Link matching, replace with [link]<br>
'(http|https)://m66.net/[a-zA-Z0-9/_-]+' => '[link]',</p>
'测试|范例' => '示例',

// Brand replacements
'苹果' => 'Apple',
'谷歌' => 'Google'

];

// Execute multi-pattern replacements
foreach ($replace_rules as $pattern => $replacement) {
$text = mb_eregi_replace($pattern, $replacement, $text);
}

// Output the result
echo nl2br($text);

Output Result

This is a sample text, containing links: [link] and [link].
Additionally, there are some brand names like Apple, Google, and other sample content.

Considerations

  1. Performance Issues: When performing a large number of replacements, calling mb_eregi_replace() in a loop may cause performance overhead. It is recommended to combine rules for a single replacement or use the array version of preg_replace().

  2. Escape Characters: When building regular expression patterns, symbols like . and / should be properly escaped.

  3. Regex Compatibility: Although mb_eregi_replace() is multibyte-safe, it does not support Perl-compatible regular expressions. For complex pattern handling, it is recommended to use preg_replace().

Alternative Solutions

Since mb_eregi_replace() has been deprecated in PHP 7.3 and later, the recommended approach is to use mb_ereg_replace() (case-sensitive) or preg_replace() (which is more powerful) as replacements. For example:

$text = preg_replace(array_keys($replace_rules), array_values($replace_rules), $text);

This makes the replacement process more concise and improves execution efficiency.

Conclusion

By abstracting the replacement rules into an array, we can use mb_eregi_replace() to quickly implement multi-rule batch text replacements. This approach is particularly suitable for simple content cleansing, keyword replacements, and content protection scenarios. Although this function is being deprecated, the concept can still be applied to newer functions or other languages to achieve efficient and flexible text processing.