Sometimes in virtual pos integrations, we can use the API information. You may be asked to specify the card type ( Visa, Mastercard, Amex, Discover, Dinners, Enroute, Jbc ) when sending the data, or you want to show the user which card type it is or you need the card type for some other reason. We can use a function that can detect card types using Regex.
Let's share our small but effective function :)
function kartTipi($number){
$number=str_replace(" ", "", $number);
$cardType = array(
"visa" => "/^4[0-9]{12}(?:[0-9]{3})?$/",
"mastercard" => "/^5[1-5][0-9]{14}$/",
"amex" => "/^3[47][0-9]{13}$/",
"discover" => "/^6(?:011|5[0-9]{2})[0-9]{12}$/",
"dinners" => "/^[300-305]d{11}$/",
"dinners" => "/^3[68]d{12}$/",
"enroute" => "/^2(014|149)d{11}$/",
"jbc" => "/^3d{15}$/",
"jbc" => "/^(2131|1800)d{11}$/",
"troy" => "/^(?:9792|65\d{2}|36|2205)\d{12}$/",
);
foreach ($cardType as $key => $value) {
if (preg_match($value,$number))
{
$type= $key;
break;
}else{
$type= false;
}
}
return $type;
}You can convert very simple codes into any shape you want.
Its usage is;
$cardNo="4546711234567894"; // VISA Test Kartı
switch (kartTipi($cartNo)) {
case 'visa':
echo "Bu bir visa karttır.";
break;
case 'mastercard':
echo "Bu bir mastercard karttır.";
break;
default:
echo "Kart tipi bulunamadı.";
break;
}Hope it will be fayal.
Stay With Love and Knowledge ...
