本文整理汇总了PHP中DataValidator::luhn_check方法的典型用法代码示例。如果您正苦于以下问题:PHP DataValidator::luhn_check方法的具体用法?PHP DataValidator::luhn_check怎么用?PHP DataValidator::luhn_check使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataValidator
的用法示例。
在下文中一共展示了DataValidator::luhn_check方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: cc_number_exists_in_str
/**
* Analyzes a string to see if any credit card numbers are hiding out in it
*
* @param $str
*
* @return bool True if a CC number was found sneaking about in the shadows
*/
public static function cc_number_exists_in_str($str)
{
$luhnRegex = <<<EOT
/
(?#amex)(3[47][0-9]{13})|
(?#bankcard)(5610[0-9]{12})|(56022[1-5][0-9]{10})|
(?#diners carte blanche)(300[0-5][0-9]{11})|
(?#diners intl)(36[0-9]{12})|
(?#diners US CA)(5[4-5][0-9]{14})|
(?#discover)(6011[0-9]{12})|(622[0-9]{13})|(64[4-5][0-9]{13})|(65[0-9]{14})|
(?#InstaPayment)(63[7-9][0-9]{13})|
(?#JCB)(35[2-8][0-9]{13})|
(?#Laser)(6(304|7(06|09|71))[0-9]{12,15})|
(?#Maestro)((5018|5020|5038|5893|6304|6759|6761|6762|6763|0604)[0-9]{8,15})|
(?#MasterCard)(5[1-5][0-9]{14})|
(?#Solo)((6334|6767)[0-9]{12,15})|
(?#Switch)((4903|4905|4911|4936|6333|6759)[0-9]{12,15})|((564182|633110)[0-9]{10,13})|
(?#Visa)(4([0-9]{15}|[0-9]{12}))
/
EOT;
$nonLuhnRegex = <<<EOT
/
(?#china union pay)(62[0-9]{14,17})|
(?#diners enroute)((2014|2149)[0-9]{11})
/
EOT;
// Transform the regex to get rid of the new lines
$luhnRegex = preg_replace('/\\s/', '', $luhnRegex);
$nonLuhnRegex = preg_replace('/\\s/', '', $nonLuhnRegex);
// Remove common CC# delimiters
$str = preg_replace('/[\\s\\-]/', '', $str);
// Now split the string on everything else and join again so the regexen have an 'easy' time
$str = join(' ', preg_split('/[^0-9]+/', $str, PREG_SPLIT_NO_EMPTY));
// First do we have any numbers that match a pattern but is not luhn checkable?
$matches = array();
if (preg_match_all($nonLuhnRegex, $str, $matches) > 0) {
return true;
}
// Find potential CC numbers that do luhn check and run 'em
$matches = array();
preg_match_all($luhnRegex, $str, $matches);
foreach ($matches[0] as $candidate) {
if (DataValidator::luhn_check($candidate)) {
return true;
}
}
// All our checks have failed; probably doesn't contain a CC number
return false;
}