Validates a credit card number, with a Luhn check if possible.
boolean Core_Valid::credit_card( integer $number [, string|array $type = null ] )
参数列表
参数 类型 描述 默认值 $number
integer
Credit card number $type
string|array
Card type, or an array of card types null
boolean
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);
}