Saturday, August 30, 2025
HomeLanguagesPHP mb_ereg_search_regs() Function

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.

  • $pattern: This is the regular expression parameter that is used to search a pattern in the given string.
  • $options: This parameter is optional. That is used to specify matching options. It can be ‘m’ for multiline, the ‘^’ and ‘$’ anchors will match the beginning and end of each line in the input string.

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




<?php
  
$text = "Welcome to neveropen";
$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




<?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




<?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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32249 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6617 POSTS0 COMMENTS
Nicole Veronica
11792 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11838 POSTS0 COMMENTS
Shaida Kate Naidoo
6731 POSTS0 COMMENTS
Ted Musemwa
7014 POSTS0 COMMENTS
Thapelo Manthata
6689 POSTS0 COMMENTS
Umr Jansen
6702 POSTS0 COMMENTS