Open In App

PHP mb_ereg() Function

The mb_ereg() function is an inbuilt function in PHP that is used for searching a string in the multibyte string by using the regular expression.

Syntax:



mb_ereg($pattern, $string, $matches ): bool

Parameters: The following function has three parameters that are described below.

Return Values: The mb_ereg() function returns either “true” or “false” depending on whether the regular expression pattern matches the string. If the optional $matches parameter is provided, any matched subpatterns will be stored in the array.



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




<?php
    
$subject = "12345This is String";
  
if(mb_ereg("^[A-Za-z\s]+$", $subject)) {
    echo "Match found!";
} else {
    echo "Match not found!";
}    
  
?>

Output:

Match not found!  

Example 2: The following code demonstrates the mb_ereg() function.




<?php
    
mb_regex_encoding('UTF-8');
$pattern = '[0-9]+[a-zA-Z]+';
$string = '123abc';
  
if (mb_ereg($pattern, $string)) {
    echo 'Pattern matched successfully!';
} else {
    echo 'Pattern did not match.';
}    
  
?>

Output:

Pattern matched successfully!  

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


Article Tags :