PHP mb_ereg_search_init() Function
Last Updated :
24 Mar, 2023
The PHP mb_ereg_search_init() function is an inbuilt function that is used to initialize the multibyte regular expression for a search. It sets the target string and regular expression to be searched using the mb_ereg_search() function.
Syntax:
bool mb_ereg_search_init(
string $string,
?string $pattern = null,
?string $options = null
)
Parameters:
- $string: The string to be searched. This parameter is required.
- $pattern: The regular expression pattern to be used for the search. It is an optional parameter, and if not specified, the previously set pattern will be used.
- $options: The search option. This parameter is optional and its default value is “msr” which stands for “multi-byte”, “single-line”, and “match-start-of-string”. The option can be set to “msri” to include a “case-insensitive” match, or “msrx” to include an “extended regular expression” match.
Return Value: This method returns “true” on success and “false” on failure.
Example 1: The following code shows the working of the PHP mb_ereg_search_init() function.
PHP
<?php
$str = "Welcome to GeeksforGeeks" ;
$pattern = "GeeksforGeeks" ;
mb_ereg_search_init( $str , $pattern );
if (mb_ereg_search()) {
echo "Pattern Match found" ;
} else {
echo "Pattern Match not found" ;
}
?>
|
Output:
Pattern Match found
Example 2: This code also shows the working of the above function.
PHP
<?php
$str = "Welcome, Geeks!" ;
$pattern = "\w+" ;
mb_ereg_search_init( $str , $pattern );
while ( $res = mb_ereg_search_regs()) {
echo "Match found: " . $res [0] . "\n" ;
}
?>
|
Output:
Match found: Welcome
Match found: Geeks
Reference: https://www.php.net/manual/en/function.mb-ereg-search-init.php
Share your thoughts in the comments
Please Login to comment...