Saturday, September 27, 2025
HomeLanguagesPHP mb_ereg_search() Function

PHP mb_ereg_search() Function

The mb_ereg_search() is a PHP function used to search & match for a regular expression pattern in a string using multi-byte characters. It is similar to preg_match() but works with multi-byte characters.

Syntax:

mb_ereg_search(?string $pattern = null, ?string $options = null): bool

Parameters: This function accepts the two parameters that are described below:

  • pattern: A regular expression pattern search for. It must be a valid regular expression for searching for a specific pattern in the string.
  • options:  This is an optional parameter. A string that specifies the search options. 

Return Value: This function returns “true” if the multibyte string matches with a given regular expression otherwise returns “false”.

Example 1:  The following code demonstrates the mb_ereg_search() function.

PHP




<?php
$string = "Hello, world!";
     
mb_regex_encoding("UTF-8");
mb_ereg_search_init($string);
     
if (mb_ereg_search("world")) {
    echo "Found 'world' in the string\n";
}
?>


Output:

Found 'world' in the string  

Example 2: The following program demonstrates the mb_ereg_serach() function.

PHP




<?php
$targetString = "The quick brown fox jumps over the lazy dog.";
$pattern = "america";
  
mb_internal_encoding("UTF-8");
mb_ereg_search_init($targetString, $pattern);
  
  
if (mb_ereg_search()) {
    echo "The pattern was found in the target string.\n";
else {
    echo "The pattern was not found in the target string.\n";
}
?>


Output:

The pattern was not found in the target string.

Reference: https://www.php.net/manual/en/function.mb-ereg-search.php

RELATED ARTICLES

Most Popular

Dominic
32323 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6690 POSTS0 COMMENTS
Nicole Veronica
11857 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11913 POSTS0 COMMENTS
Shaida Kate Naidoo
6806 POSTS0 COMMENTS
Ted Musemwa
7073 POSTS0 COMMENTS
Thapelo Manthata
6763 POSTS0 COMMENTS
Umr Jansen
6768 POSTS0 COMMENTS