Open In App

PHP mb_ereg_search_regs() Function

The mb_ereg_search_regs() function is an inbuilt function in PHP that is used for regular expressions to match a given string. If the match is found, then it will return the matched part as an array.

Syntax:



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

Parameters: This function accepts two parameters that are described below.

Return Values: The mb_ereg_search_regs() function returns an array that contains the matched part of the multibyte regular expression. If the function successfully executes, it returns “true”, otherwise this function returns “false”.



Program 1: The following program demonstrates the mb_ereg_search_regs() function.




<?php
  
$text = "Welcome to GeeksforGeeks";
$pattern = "Welcome";
  
// Set the multibyte encoding
mb_regex_encoding("UTF-8");
  
// Initialize the search
$bool = mb_ereg_search_init($text);
  
$array = mb_ereg_search_regs("$pattern");
  
var_dump($array);
  
?>

Output
array(1) {
  [0]=>
  string(7) "Welcome"
}

Program 2: The following program demonstrates the mb_ereg_search_regs() function.




<?php
    
$text = "Food is tasty. The sun is shining";
$pattern = "Geeks for Geeks";
  
// Set the multibyte encoding
mb_regex_encoding("UTF-8");
  
// Initialize the search
$bool = mb_ereg_search_init($text);
  
if (mb_ereg_search_regs($pattern)) {
    echo "Pattern is Found";
} else {
    echo "Pattern is not found";
}
  
?>

Output
Pattern is not found

Program 3: The following program demonstrates the mb_ereg_search_regs() function.




<?php
  
$text = "I have 10 apples and 5 oranges.";
$pattern = "(\d+) (\w+)";
  
// Set the multibyte encoding
mb_regex_encoding("UTF-8");
  
// Initialize the search
$bool = mb_ereg_search_init($text);
  
if ($bool) {
    if (mb_ereg_search($pattern)) {
        $result = mb_ereg_search_regs();
        echo "Quantity: " . $result[1] . "\n";
        echo "Fruit: " . $result[2] . "\n";
    } else {
        echo "Pattern is not found";
    }
} else {
    echo "Failed to initialize search";
}
  
?>

Output
Quantity: 5
Fruit: oranges

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


Article Tags :