Content · Programming · PHP

PHP mb_ereg_search_regs() Function: Usage and Examples

Learn how to retrieve captured groups for the next multibyte regex match in PHP with syntax, clear code examples, practical use, and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

When working with PHP, you may need to retrieve captured groups for the next multibyte regex match. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.

mb_ereg_search_regs: Capture arrays include the complete matched text and any available groups.

What Is mb_ereg_search_regs in PHP?

Retrieve captured groups for the next multibyte regex match. Capture arrays include the complete matched text and any available groups.

Syntax:

mb_ereg_search_regs(?string $pattern = null, ?string $options = null): array|false

Parameters and return value: The signature above identifies the arguments and return type where the API has a single call. For topic guides, follow the sequence of calls in the examples. Always distinguish a returned value from an object or variable modified by reference.

Important: Check that matching succeeded before accessing group indexes; not every expression has the same captures.

Example 1: Basic Usage

This example introduces the operation with a short and predictable input.

<?php

mb_ereg_search_init("item-42");
print_r(mb_ereg_search_regs("([a-z]+)-([0-9]+)"));

The code focuses on the main operation. Capture arrays include the complete matched text and any available groups.

Example 2: Another Case

Now compare a different input, locale, date, or option to see another part of the behavior.

<?php

mb_ereg_search_init("unknown");
var_dump(mb_ereg_search_regs("([0-9]+)"));

What changes? Check that matching succeeded before accessing group indexes; not every expression has the same captures.

Practical Example: Applying It in an Application

Use the following separate example as a starting point for a small application task. Check any extension or environment requirements below before running it.

<?php

$reference = "INV-2026";
mb_ereg_search_init($reference);
$groups = mb_ereg_search_regs("([A-Z]+)-([0-9]+)");
if ($groups !== false) {
    echo "Type: " . $groups[1] . ", Number: " . $groups[2];
}

The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. Capture arrays include the complete matched text and any available groups.

Important Behavior and Edge Cases

  • Core behavior: Capture arrays include the complete matched text and any available groups.
  • Inputs and results: Check that matching succeeded before accessing group indexes; not every expression has the same captures.
  • Alternatives: mb_ereg_search_pos() returns positions and lengths instead of captured substrings.
  • Compatibility: mbstring is required for mb_* functions.

Comparison With Related Functions

Function or ApproachWhen to Use It
mb_ereg_search_regsRetrieve captured groups for the next multibyte regex match.
Related PHP approachmb_ereg_search_pos() returns positions and lengths instead of captured substrings.

Common Beginner Mistakes

MistakeCorrect Approach
Using an incorrect input or optionCheck that matching succeeded before accessing group indexes; not every expression has the same captures.
Confusing this API with a related operationmb_ereg_search_pos() returns positions and lengths instead of captured substrings.
Assuming output is identical across environmentsCheck return values and required extensions or versions. mbstring is required for mb_* functions.

Key Takeaways

  • mb_ereg_search_regs helps you retrieve captured groups for the next multibyte regex match.
  • Capture arrays include the complete matched text and any available groups.
  • Check that matching succeeded before accessing group indexes; not every expression has the same captures.

Use mb_ereg_search_regs when you need to retrieve captured groups for the next multibyte regex match. Start with the first example, compare the second, and adapt the practical example to your inputs. Confirm the return value and environment requirements before adding it to production code.

Share this resourceSend this page to someone who may find it useful.