选择语言 :

 Core_Valid::credit_card

Validates a credit card number, with a Luhn check if possible.

boolean Core_Valid::credit_card( integer $number [, string|array $type = null ] )
uses
Validate::luhn

参数列表

参数 类型 描述 默认值
$number integer Credit card number
$type string|array Card type, or an array of card types null
返回值
  • boolean
File: ./core/classes/valid.class.php
public static function credit_card($number, $type = null)
{
    // Remove all non-digit characters from the number
    if ( ($number = preg_replace('/\D+/', '', $number)) === '' ) return false;

    if ($type == null)
    {
        // Use the default type
        $type = 'default';
    }
    elseif (is_array($type))
    {
        foreach ($type as $t)
        {
            // Test each type for validity
            if (Valid::credit_card($number, $t)) return true;
        }

        return false;
    }

    $cards = Core::config('credit_cards');

    // Check card type
    $type = strtolower($type);

    if (!isset($cards[$type])) return false;

    // Check card number length
    $length = strlen($number);

    // Validate the card length by the card type
    if (!in_array($length, preg_split('/\D+/', $cards[$type]['length']))) return false;

    // Check card number prefix
    if (!preg_match('/^' . $cards[$type]['prefix'] . '/', $number)) return false;

    // No Luhn check required
    if ($cards[$type]['luhn'] == false) return true;

    return Valid::luhn($number);
}