Open In App

PHP mb_ereg_search_getregs() Function

The mb_ereg_search_getregs() function is an inbuilt function in PHP that is used for matching substrings in the last multibyte regular expression match. It can be used after calling mb_ereg_search() or mb_ereg_search_pos() functions.

Syntax:



mb_ereg_search_getregs(): array|false

Parameters: This function does not accept any parameters.

Return Values: This function returns an array of the matched substring in the last multibyte regular expression match. The first element of the array contains the entire matched substring, and the subsequent elements contain the contents of any capturing parentheses in the regular expression. It returns “false” when the error occurs.



Example 1: The following program demonstrates the mb_ereg_serach_getregs() function.




<?php
$string = "The quick brown fox jumps over the lazy dog";
$pattern = "fox";
mb_regex_encoding("UTF-8");
mb_ereg_search_init($string, $pattern);
if (mb_ereg_search()) {
    $regs = mb_ereg_search_getregs();
    echo "The matched substring is:" . $regs[0];
}
?>

Output:

The matched substring is: fox  

Example 2: The following program demonstrates the mb_ereg_search_getregs() function.




<?php
$string = "Geeks for Geeks";
$pattern = "[m]";
mb_regex_encoding("UTF-8");
mb_ereg_search_init($string, $pattern);
if (mb_ereg_search()) {
    $regs = mb_ereg_search_getregs();
    echo "The matched substring is: " . $regs[0] . "\n";
} else {
    echo "Match string is not found";
}
?>

Output:

Match string is not found 

Reference: https://www.php.net/manual/en/function.mb-ereg-search-getregs.php


Article Tags :