Php preg_match的问题

Php preg_match的问题,php,cakephp,preg-match,Php,Cakephp,Preg Match,我必须验证激光信用卡的有效性。该卡以6304、6706、6709、6771开头,长度为16或19位。我有一个preg_匹配,我传入的卡号从6706开始,有19位数字,但返回false // Laser (Laser) P: 6304, 6706, 6709, 6771 L: 16,19 } elseif (preg_match('/^(?:[6304|6706|6709|6771])\d{12,15}$/', $number)) { $type = 'laser'

我必须验证激光信用卡的有效性。该卡以6304、6706、6709、6771开头,长度为16或19位。我有一个preg_匹配,我传入的卡号从6706开始,有19位数字,但返回false

    // Laser (Laser) P: 6304, 6706, 6709, 6771 L: 16,19
    } elseif (preg_match('/^(?:[6304|6706|6709|6771])\d{12,15}$/', $number)) {
        $type = 'laser';
细分:

/^                        # 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
要指出您所犯的错误:

(?:[6304 | 6706 | 6709 | 6771])

请记住,
[]
是一个类。这意味着要在括号内查找这些字符中的任何一个。如果您要选择或,则需要使用组
()

修复了它的外观:
(?:6304 | 6706 | 6709 | 6771)

\d{12,15}


我的理解是,你需要的是固定长度的数字,而不是可变长度的数字。你的量词是说它可以是12,13,…,15个以上的数字。我们只想再要12或15个。

谢谢,这就够了。我对preg_match有了更好的理解
/^                        # 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