Current Location: Home> Latest Articles> Techniques for Replacing Multiple Different Substrings with mb_ereg_replace: Key to Boosting Code Efficiency

Techniques for Replacing Multiple Different Substrings with mb_ereg_replace: Key to Boosting Code Efficiency

gitbox 2025-09-17

<?php
/*
Article Title: Techniques for Replacing Multiple Different Substrings with mb_ereg_replace: Key to Boosting Code Efficiency
*/

echo "

mb_ereg_replace Techniques and Efficient Practices for Replacing Multiple Substrings

";

// 1. Overview of mb_ereg_replace
echo "

In PHP, the mb_ereg_replace function is used for regex-based replacement of multibyte strings, making it particularly suitable for Chinese or other multibyte character environments. Its basic usage is as follows:

"
;
echo '
$newString = mb_ereg_replace($pattern, $replacement, $string);
'
;

// 2. Single Substring Replacement Example
echo "

For example, replacing “apple” with “banana” in a string:

"
;
$string = "I like apples and apple juice";
$newString = mb_ereg_replace("apple", "banana", $string);
echo "
Original string: <span>$string</span></span></span><span>\nAfter replacement: </span><span><span>$newString</span></span><span>
"
;

// 3. Techniques for Replacing Multiple Different Substrings
echo "

Technique 1: Using Regex or Pipe Symbol

"
;
echo "

You can match multiple substrings at once using a regex pattern with the pipe '|' symbol:

"
;
$string = "I like apples, oranges, and bananas";
$pattern = "apple|orange|banana";
$replacement = "fruit";
$newString = mb_ereg_replace($pattern, $replacement, $string);
echo "
Original string: <span>$string</span></span></span><span>\nAfter replacement: </span><span><span>$newString</span></span><span>
"
;

echo "

Technique 2: Using Array Loop for Batch Replacement

"
;
echo "

If each substring has a different replacement, you can loop through an array:

"
;
$string = "I like apples, oranges, and bananas";
$replacements = [
"apple" => "apple pie",
"orange" => "orange juice",
"banana" => "banana smoothie"
];
foreach ($replacements as $search => $replace) {
$string = mb_ereg_replace($search, $replace, $string);
}
echo "
After replacement: <span>$string</span></span></span><span>
"
;

echo "

Technique 3: Building a Dynamic Regex Replacement Function

"
;
echo "

Use an anonymous function to handle more complex replacement logic:

"
;
$string = "I like apples, oranges, and bananas";
$pattern = "apple|orange|banana";
$map = [
"apple" => "Apple",
"orange" => "Orange",
"banana" => "Banana"
];
$newString = mb_ereg_replace($pattern, function($matches) use ($map) {
return $map[$matches[0]];
}, $string);
echo "
After replacement: <span>$newString</span></span></span><span>
"
;

// 4. Key Points to Boost Code Efficiency
echo "

Key Points for Improving Efficiency

"
;
echo "

  • Minimize the number of loops and prioritize regex to replace multiple substrings at once.
  • For complex replacement rules where each substring differs, use a mapping array with a callback function to keep code clear.
  • Be aware that mb_ereg_replace can be slower on large datasets; consider using mb_ereg_replace_callback when optimization is needed.
"
;

echo "

Summary: Mastering regex with the pipe symbol, array loop replacement, and callback functions allows you to efficiently use mb_ereg_replace to replace multiple different substrings while ensuring multibyte character safety.

";
?>