Correctly Detect Credit Card Type Php.In this article I will show you how you correctly detect credit card type in you php function. Almost all credit cards can be validating using their regular expression. Following is the simple php function using that function in your site code you can easily detect the CC type.
23 October, 2018
Correctly Show Detect Credit Card Type In Php
Programing Coderfunda October 23, 2018 Credit Card, php No comments
Correctly Detect Credit Card Type Php.In this article I will show you how you correctly detect credit card type in you php function. Almost all credit cards can be validating using their regular expression. Following is the simple php function using that function in your site code you can easily detect the CC type.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
function getCreditCardType($str, $format = 'string')
{
if (empty($str)) {
return false;
}
$matchingPatterns = [
'visa' => '/^4[0-9]{12}(?:[0-9]{3})?$/',
'mastercard' => '/^5[1-5][0-9]{14}$/',
'amex' => '/^3[47][0-9]{13}$/',
'diners' => '/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/',
'discover' => '/^6(?:011|5[0-9]{2})[0-9]{12}$/',
'jcb' => '/^(?:2131|1800|35\d{3})\d{11}$/',
'any' => '/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}
|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]| [68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/'
];
$ctr = 1;
foreach ($matchingPatterns as $key=>$pattern) {
if (preg_match($pattern, $str)) {
return $format == 'string' ? $key : $ctr;
}
$ctr++;
}
}
|