Open In App

PHP mb_ereg_search_init() Function

Last Updated : 24 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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
  
// Declare a string
$str = "Welcome to GeeksforGeeks";
  
// Declare a pattern string
$pattern = "GeeksforGeeks";
  
// Using mb_ereg_search_init() function
mb_ereg_search_init($str, $pattern);
  
// Use mb_ereg_search() function for
// multibyte regular expression match
// for a predefined multibyte string
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
  
// Declare the search string
$str = "Welcome, Geeks!";
  
// Non-alphanumeric characters
$pattern = "\w+";
  
// Using the mb_ereg_search_init() function to
// search the multibyte regular expression
mb_ereg_search_init($str, $pattern);
  
// Perform the search and print the results
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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads