Skip to content

Instantly share code, notes, and snippets.

@eneiasramos
Forked from claudiosanches/test.php
Created October 26, 2019 05:47
Show Gist options
  • Save eneiasramos/b9b99f090d273a9f9cea83d9a10cb4bf to your computer and use it in GitHub Desktop.
Save eneiasramos/b9b99f090d273a9f9cea83d9a10cb4bf to your computer and use it in GitHub Desktop.
Regex for test credit card brand
<?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