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

3 COMMENTS

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS