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 ); ?> |
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" ; } ?> |
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" ; } ?> |
Quantity: 5 Fruit: oranges
Reference: https://www.php.net/manual/en/function.mb-ereg-search-regs.php