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.
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.
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.
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"; }
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"; }
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"; ?>