Current Location: Home> Latest Articles> How to Use the mb_eregi_replace Function to Standardize Phrase Replacement in Multilingual Projects?

How to Use the mb_eregi_replace Function to Standardize Phrase Replacement in Multilingual Projects?

M66 2025-06-23

When building a website with multilingual support, we often need to replace specific phrases or tags on pages based on different language environments. The mb_eregi_replace() function, a multibyte-character-friendly regular expression replacement function in PHP, is perfect for handling Unicode text, especially languages with non-Latin characters (such as Chinese, Japanese, Arabic, etc.).

This article will explain how to use the mb_eregi_replace() function to standardize the replacement of specific language phrases in internationalized projects, and provide practical examples to demonstrate its usage.

1. What is mb_eregi_replace?

mb_eregi_replace() is a function in PHP’s Multibyte String (mbstring) extension that performs case-insensitive regular expression replacement. It is similar to the standard eregi_replace(), but it supports UTF-8 encoding, making it suitable for internationalization scenarios.

The syntax is as follows:

string mb_eregi_replace(string $pattern, string $replacement, string $string [, string $option])
  • $pattern: The regular expression to match.

  • $replacement: The string to replace the matched text with.

  • $string: The source string in which the replacement will occur.

  • $option: (Optional) Specifies the encoding (e.g., "UTF-8").

2. Typical Internationalization Use Case

Suppose we have a template string containing placeholders or phrases, and based on the current language environment (e.g., en, zh, fr), we need to replace them with the corresponding content in that language.

Example: Replacing Internationalized Placeholders

Set a simple template string as follows:

$template = "Welcome to [SITE_NAME], click [LINK] for more information.";

Now, we want to replace [SITE_NAME] and [LINK] with values corresponding to different languages based on the language environment.

Step 1: Define the Phrase Table for Each Language

$phrases = [
    'zh' => [
        'SITE_NAME' => 'M66网站',
        'LINK' => '<a href="https://m66.net/zh/about">这里</a>'
    ],
    'en' => [
        'SITE_NAME' => 'M66 Site',
        'LINK' => '<a href="https://m66.net/en/about">here</a>'
    ]
];

Step 2: Construct the Regular Expression Replacement Logic

function localize_template($template, $language, $phrases) {
    foreach ($phrases[$language] as $key => $value) {
        // Use mb_eregi_replace for replacement
        $template = mb_eregi_replace('\[' . $key . '\]', $value, $template, 'UTF-8');
    }
    return $template;
}

Step 3: Test the Output

$language = 'zh';
echo localize_template($template, $language, $phrases);

Output:

欢迎访问M66网站,请点击<a href="https://m66.net/zh/about">这里</a>获取更多信息。

3. Considerations

  1. Performance: mb_eregi_replace() may have lower performance than the regular str_replace() when handling large amounts of text, due to its multibyte character and case-insensitive matching. Choose based on your needs.

  2. Escape Characters: Square brackets [ ] in the replacement target are special characters in regular expressions and must be escaped with a backslash, or the match will fail.

  3. Encoding Consistency: Ensure that all strings are using UTF-8 encoding. Mixed encodings may cause replacement to fail.

4. Application Extension in Template Systems

In more complex internationalization projects, we can encapsulate the above method into a language processing module to scan template files in bulk and dynamically replace text based on language package files. We can even combine caching mechanisms to improve efficiency.

For example, you can load the corresponding language package based on the directory structure:

$language_file = __DIR__ . "/lang/{$language}.php";
if (file_exists($language_file)) {
    $phrases = include $language_file;
}

5. Conclusion

mb_eregi_replace() is an extremely useful text replacement tool in internationalization projects, particularly for handling strings containing multibyte characters. By using it flexibly, we can dynamically replace language phrases in templates, thus building a truly multilingual support system. With a unified phrase library and template engine, the entire internationalization architecture becomes efficient and easy to maintain.