Sunday, September 22, 2024
Google search engine
HomeLanguagesPHP mb_ereg() Function

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.

  • $pattern:  The regular expression pattern that we used for matching against the multibyte string.
  • $string:  The string to search for a match.
  • $matches:  This is the optional parameter that stored the match elements in the array.

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




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




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

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

Recent Comments