problem with preg_match
I have to validate for a Laser Credit card. The card starts with either 6304, 6706, 6709, 6771 and is 16 or 19 digits in length. I have a preg_match and I am passing in the card number starting with 6706 and has 19 digits but it returns false.
// Laser (Laser) P: 6304, 6706, 6709, 6771 L: 16,19
} elseif (preg_match('/^(?:[6304|6706|6709|6771])\d{12,15}$/', $number)) {
$type = 'laser';
开发者_运维百科
/^6(?:304|706|709|771)(?:\d{12}|\d{15})$/
Broken down:
/^ # start of line
6(?:304|706|709|771) # your 6xxx codes
(?:\d{12}|\d{15}) # 12 (16-4) or 15 (19-4) more numbers
$/ # end of pattern
To point out the mistakes you had:
(?:[6304|6706|6709|6771])
Remember that []
is a CLASS. That means to look for any of those characters within the brackets. If you're going for either/or, you need to use a group ()
.
Fixed it should look like: (?:6304|6706|6709|6771)
\d{12,15}
My understanding is you need fixed-length of numbers, not a variable one. Your quantifier is saying it can be 12, 13, ..., 15 more numbers. We only want 12 OR 15 more.
精彩评论