Current Location: Home> Latest Articles> Common Regular Expression Examples and Tips for mb_ereg_match Function

Common Regular Expression Examples and Tips for mb_ereg_match Function

gitbox 2025-08-27

Common Regular Expression Examples and Tips for mb_ereg_match Function

In PHP, this function is used for regular expression matching in multibyte strings, supporting UTF-8 and other multibyte encodings. It is commonly used to handle texts containing Chinese, Japanese, and other non-ASCII characters. This article will introduce some common regex examples and practical tips.

1. Basic Usage

The syntax is as follows:

bool mb_ereg_match(string $pattern, string $string[, string $option = ""])

Returns true if the match is successful, false if the match fails.

Example:

mb_regex_encoding("UTF-8"); // Set the encoding for regex matching
if (mb_ereg_match("^[0-9]+$", "12345")) {
    echo "Match successful";
} else {
    echo "Match failed";
}

In this example, the regex ^[0-9]+$ indicates that the string must consist entirely of digits.

2. Matching Chinese Characters

Chinese characters can be matched using the Unicode range:

mb_regex_encoding("UTF-8");
if (mb_ereg_match("^[\x{4e00}-\x{9fa5}]+$", "测试中文")) {
    echo "Fully Chinese match";
} else {
    echo "Match failed";
}

Here, [\x{4e00}-\x{9fa5}] represents the common Unicode range for Chinese characters.

3. Matching Email Addresses

Email matching example:

mb_regex_encoding("UTF-8");
$pattern = "^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$";
if (mb_ereg_match($pattern, "[email protected]")) {
    echo "Valid email";
} else {
    echo "Invalid email";
}

4. Matching Phone Numbers

Common format for Chinese mainland phone numbers:

mb_regex_encoding("UTF-8");
$pattern = "^1[3-9][0-9]{9}$";
if (mb_ereg_match($pattern, "13800138000")) {
    echo "Valid phone number";
} else {
    echo "Invalid phone number";
}

5. Practical Tips

  • Set encoding: Before using mb_ereg functions, always use mb_regex_encoding("UTF-8") to set the character encoding, otherwise Chinese characters may fail to match.
  • Use anchors: ^ represents the start, $ represents the end, ensuring the entire string matches.
  • Combine patterns: You can use | to combine multiple matching rules, e.g., ^(apple|orange|banana)$.
  • Escape special characters: Special characters in regex such as ., +, * need to be escaped as needed.
  • Performance note: mb_ereg functions are slightly slower than preg functions, but safer and more reliable for multibyte strings.

Summary: mb_ereg_match is a powerful tool for regex matching in multibyte strings. By properly setting encoding and regex rules, you can easily match numbers, Chinese characters, emails, phone numbers, and other common needs. Mastering these examples and tips will make your PHP string handling more robust.

<?php // This part is unrelated to the article content and can be used for script termination echo "\nPHP script execution completed.\n"; ?>