Function name: mb_ereg()
Function Description: The mb_ereg() function is used to perform multibyte regular expression matching.
Usage: mb_ereg(string $pattern, string $string [, array &$regs])
parameter:
- $pattern: The regular expression pattern to match.
- $string: The string in which to search for pattern.
- $regs (optional): an array containing matching results. If this parameter is provided, the function will store the matching result in the array.
Return value: If the match is successful, the function returns true, otherwise false.
Example:
// 在字符串中搜索匹配的正则表达式$string = "Hello, 你好!"; $pattern = "你好"; if (mb_ereg($pattern, $string)) { echo "匹配成功"; } else { echo "匹配失败"; } // 使用数组存储匹配结果$string = "Hello, 你好!"; $pattern = "([A-Za-z]+),\s+(\p{Han}+)"; $regs = array(); if (mb_ereg($pattern, $string, $regs)) { echo "匹配成功"; echo "完整匹配结果:" . $regs[0] . PHP_EOL; echo "第一个括号内的匹配结果:" . $regs[1] . PHP_EOL; echo "第二个括号内的匹配结果:" . $regs[2] . PHP_EOL; } else { echo "匹配失败"; }
Notes:
- The mb_ereg() function is multibyte character-safe and can handle strings containing multibyte characters.
- The behavior of this function is affected by the current regular expression encoding set by the mb_regex_encoding() function.
- Before using the mb_ereg() function, you need to make sure that the correct regular expression encoding has been set through the mb_regex_encoding() function.
- If you want to perform case-insensitive matching, you can use the "i" modifier such as "/pattern/i" in the pattern string.
- If you want to perform a global matching, you can use the "g" modifier such as "/pattern/g" in the pattern string.
- If you want to perform multi-line matching, you can use the "m" modifier such as "/pattern/m" in the pattern string.
- To improve performance, the mb_ereg_match() function can be used to perform a single match without returning an array of matching results.
- In order to better understand and use this function, it is recommended to refer to the detailed description and examples of the mb_ereg() function in the official PHP documentation.