Open In App

PHP mb_ereg_search_init() Function

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:

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


Article Tags :