Current Location: Home> Latest Articles> How to Use $1 as Replacement in PHP's mb_eregi_replace Function When There Are No Groups in the Pattern

How to Use $1 as Replacement in PHP's mb_eregi_replace Function When There Are No Groups in the Pattern

M66 2025-06-16

In PHP, the mb_eregi_replace function is a multibyte-safe regular expression replacement function that supports case-insensitive regex replacements. Its usage is similar to eregi_replace, but optimized for multibyte strings.


Problem Background

When using mb_eregi_replace for replacement, a common scenario is to use $1 in the replacement string to reference the content of the first captured group, like this:

$pattern = 'pattern';
$replacement = '$1 something';
mb_eregi_replace($pattern, $replacement, $subject);

However, if there are no groups defined in the regular expression pattern (i.e., no subpatterns enclosed in parentheses), then $1 has no corresponding content. In this case, $1 will be treated as a normal string and will not be replaced with any matching content.

This can lead to unexpected replacement results.


Why Does This Problem Occur?

$1, $2, and other special symbols are used to reference the content of capture groups in regular expressions. If no parentheses are used in the regex to define groups, these references are invalid.

For example:

$pattern = 'hello';
$replacement = '$1 world';
$subject = 'hello';
<p>// No groups, $1 has no corresponding content<br>
echo mb_eregi_replace($pattern, $replacement, $subject);<br>

The output of this code is:

$1 world

Rather than the expected:

hello world

Solution

To use $1 in the replacement result, you must ensure that the regular expression contains the corresponding capture groups.

Solution 1: Add Groups to the Regular Expression

For example, modify the pattern as follows:

$pattern = '(hello)';
$replacement = '$1 world';
$subject = 'hello';
<p>echo mb_eregi_replace($pattern, $replacement, $subject);<br>

The output is:

hello world

Note:

  • Use parentheses to enclose the part you want to reference, which defines a capture group.

  • Even if only the entire match is captured, $1 can still be used.


Solution 2: Use $0 to Reference the Entire Match

In some regular expression replacement functions, $0 represents the entire matched string. Unfortunately, mb_eregi_replace does not support $0, and you can only use group references such as $1, $2, etc.


Solution 3: Use a Callback Instead of $1

If it is inconvenient to modify the pattern to add groups, you can use a replacement function that supports callbacks, such as mb_ereg_replace_callback, and handle the matching logic in the callback.

Example:

$pattern = 'hello';
$subject = 'hello';
<p>$result = mb_ereg_replace_callback($pattern, function ($matches) {<br>
// $matches[0] is the entire match<br>
return $matches[0] . ' world';<br>
}, $subject);</p>
<p>echo $result;<br>

Output:

hello world

Summary

  • mb_eregi_replace uses $1, $2 to reference group contents, so ensure that the regular expression contains the corresponding capture groups.

  • When there are no groups, $1 will not be replaced with matching content and will be treated as a regular string.

  • If you do not want to or cannot add groups, it is recommended to use a callback function for complex replacements.


Reference Links