0

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:

  1. should contain 11 digits
  2. should starts with 9477 or 9476

Here is my code:

preg_match('/^9477\d{7}$/',$Mnumber)
Toto
  • 89,455
  • 62
  • 89
  • 125
colombo
  • 520
  • 2
  • 9
  • 24

3 Answers3

1

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

Toto
  • 89,455
  • 62
  • 89
  • 125
1

Use grouping in []

echo preg_match('/^947[76]\d{7}$/',$Mnumber);
Richard
  • 1,045
  • 7
  • 11
0

Just use (9477|9476)

echo preg_match('/^(9477|9476)\d{7}$/',$Mnumber);

You can also use:

  1. /^947(7|6)\d{7}$/
  2. /^947[76]\d{7}$/
Thamilhan
  • 13,040
  • 5
  • 37
  • 59