Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
PHP – mb_eregi() function
The mb_eregi() function in PHP performs case-insensitive regular expression matching with multibyte support. Unlike mb_ereg(), it ignores case when comparing patterns, making it useful for international text processing.
Syntax
int mb_eregi( string $pattern, string $string, array &$matches = null )
Parameters
pattern − The regular expression pattern to match (case-insensitive)
string − The input string to search within
matches − Optional array to store matched substrings. If matches are found, they are stored in this array
Return Value
Returns the number of bytes of the matched string if successful, or FALSE if no match is found.
Example 1: Basic Case-Insensitive Matching
This example demonstrates case-insensitive pattern matching −
<?php
// Set UTF-8 encoding for multibyte support
mb_regex_encoding("UTF-8");
// Case-insensitive search for "WORLD"
$result1 = mb_eregi("WORLD", "Hello World");
echo "Pattern 'WORLD' found: " . ($result1 ? "Yes" : "No") . "<br>";
// Case-insensitive search for "hello"
$result2 = mb_eregi("hello", "Hello World");
echo "Pattern 'hello' found: " . ($result2 ? "Yes" : "No") . "<br>";
// Pattern not found
$result3 = mb_eregi("xyz", "Hello World");
echo "Pattern 'xyz' found: " . ($result3 ? "Yes" : "No");
?>
Pattern 'WORLD' found: Yes Pattern 'hello' found: Yes Pattern 'xyz' found: No
Example 2: Using Matches Array
This example shows how to capture matched substrings −
<?php
mb_regex_encoding("UTF-8");
$text = "Contact us at SUPPORT@EXAMPLE.COM";
$pattern = "([a-z]+)@([a-z]+\.[a-z]+)";
$result = mb_eregi($pattern, $text, $matches);
if ($result) {
echo "Full match: " . $matches[0] . "<br>";
echo "Username: " . $matches[1] . "<br>";
echo "Domain: " . $matches[2];
} else {
echo "No email found";
}
?>
Full match: SUPPORT@EXAMPLE.COM Username: SUPPORT Domain: EXAMPLE.COM
Comparison: mb_eregi() vs mb_ereg()
| Function | Case Sensitivity | Pattern "HELLO" vs "hello" |
|---|---|---|
mb_ereg() |
Case-sensitive | Different |
mb_eregi() |
Case-insensitive | Same |
Conclusion
The mb_eregi() function is essential for case-insensitive pattern matching in multibyte strings. It's particularly useful when working with user input or international text where case variations need to be ignored.
