Thursday, September 4, 2025
HomeLanguagesPHP mb_ereg_search_init() Function

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:

  • $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 neveropen";
  
// Declare a pattern string
$pattern = "neveropen";
  
// 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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6628 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11858 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS