PHP – 使用 mb_ereg_match() 匹配正则表达式
在 PHP 中,**mb_ereg_match()** 函数用于将给定字符串与正则表达式模式进行匹配。此函数仅匹配字符串的开头部分,并不一定匹配到字符串的结尾。如果找到匹配项,则此函数将返回 true 或 1,否则返回 False 或 0。
语法
bool mb_ereg_match(str $pattern, str $string, str $options)
参数
它接受以下三个参数:
**$pattern** − 此参数用于正则表达式。
**$string** − 此参数正在被评估。
**$options** − 它用于搜索。
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
返回值
如果给定字符串与正则表达式模式匹配,则 **mb_ereg_match()** 返回 true 或 1。如果它不匹配,则返回 False 或 0。
示例 1
<?php //It will return True because H is matched $result = mb_ereg_match("H", "Hello World"); var_dump($result); //It will return Frue because H is not matched $output= mb_ereg_match("H", "World"); var_dump($output); ?>
输出
bool(true) bool(false)
**注意** − 在此示例中,它只匹配字符串的开头,但不一定匹配到字符串的结尾。
如果要在给定字符串中的任何位置匹配字符串,则可以使用通配符和重复运算符 .*。请参见下一个示例。
示例 2
<?php $result = mb_ereg_match(".*e", "Hello World"); var_dump($result); ?>
输出
bool(true)
广告