本文整理汇总了PHP中Valid::cards_config方法的典型用法代码示例。如果您正苦于以下问题:PHP Valid::cards_config方法的具体用法?PHP Valid::cards_config怎么用?PHP Valid::cards_config使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Valid
的用法示例。
在下文中一共展示了Valid::cards_config方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: credit_card
/**
* Validates a credit card number using the Luhn (mod10) formula.
* @see http://en.wikipedia.org/wiki/Luhn_algorithm
*
* @param integer credit card number
* @param string|array card type, or an array of card types
* @return 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 = Valid::cards_config();
// 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;
}
// Checksum of the card number
$checksum = 0;
for ($i = $length - 1; $i >= 0; $i -= 2) {
// Add up every 2nd digit, starting from the right
$checksum += substr($number, $i, 1);
}
for ($i = $length - 2; $i >= 0; $i -= 2) {
// Add up every 2nd digit doubled, starting from the right
$double = substr($number, $i, 1) * 2;
// Subtract 9 from the double where value is greater than 10
$checksum += $double >= 10 ? $double - 9 : $double;
}
// If the checksum is a multiple of 10, the number is valid
return $checksum % 10 === 0;
}