-
-
Save edvaldoszy/e06feb26e1a465954bb8e4f75008fd03 to your computer and use it in GitHub Desktop.
Regex for test credit card brand
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Test cards | |
$cards = array( | |
'378282246310005', // American Express | |
'371449635398431', // American Express | |
'5078601870000127985', // Aura | |
'5078601800003247449', // Aura | |
'30569309025904', // Diners Club | |
'38520000023237', // Diners Club | |
'6011111111111117', // Discover | |
'6362970000457013', // Elo | |
'6062825624254001', // Hipercard | |
'5555555555554444', // MasterCard | |
'5105105105105100', // MasterCard | |
'6759649826438453', // Maestro | |
'6799990100000000019', // Maestro | |
'4111111111111111', // Visa | |
'4012888888881881', // Visa | |
); | |
// Brands regex | |
$brands = array( | |
'visa' => '/^4\d{12}(\d{3})?$/', | |
'mastercard' => '/^(5[1-5]\d{4}|677189)\d{10}$/', | |
'diners' => '/^3(0[0-5]|[68]\d)\d{11}$/', | |
'discover' => '/^6(?:011|5[0-9]{2})[0-9]{12}$/', | |
'elo' => '/^((((636368)|(438935)|(504175)|(451416)|(636297))\d{0,10})|((5067)|(4576)|(4011))\d{0,12})$/', | |
'amex' => '/^3[47]\d{13}$/', | |
'jcb' => '/^(?:2131|1800|35\d{3})\d{11}$/', | |
'aura' => '/^(5078\d{2})(\d{2})(\d{11})$/', | |
'hipercard' => '/^(606282\d{10}(\d{3})?)|(3841\d{15})$/', | |
'maestro' => '/^(?:5[0678]\d\d|6304|6390|67\d\d)\d{8,15}$/', | |
); | |
// Run test | |
foreach ( $cards as $number ) { | |
$brand = 'undefined'; | |
foreach ( $brands as $_brand => $regex ) { | |
if ( preg_match( $regex, $number ) ) { | |
$brand = $_brand; | |
break; | |
} | |
} | |
echo '<pre>' . print_r( array( $number, $brand ), true ) . '</pre>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment