I have return a preg_match to check whether starting digits were 9477
or not and it works correctly. But now I want add another condition to add 9477
or 9476
should be valid.
Here is the condition:
9477
or 9476
Here is my code:
preg_match('/^9477\d{7}$/',$Mnumber)
Use an alternation between the two numbers:
preg_match('/^947(?:7|6)\d{7}$/',$Mnumber)
(?:7|6)
is a non capture group that matches digit 7
or 6
. A non capture group is much more efficient than a capture group.
You can do also:
preg_match('/^947[76]\d{7}$/',$Mnumber);
[67]
is a character class that matches digit 7
or 6
在[]中使用分组
echo preg_match('/^947[76]\d{7}$/',$Mnumber);
Just use (9477|9476)
echo preg_match('/^(9477|9476)\d{7}$/',$Mnumber);
You can also use:
/^947(7|6)\\d{7}$/
/^947[76]\\d{7}$/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.