Recently while writing some billing functions I came across the need to show customers the credit card number on file for their account. Since most people would freak out if I showed them the entire number I had to make a quick function to obfuscate the number and let them know what type of card they previously used. Here is a quick way to do just that.
This function is super easy to use and it should reliably obfuscate your cards and return the proper card type based on the card number. Call it like this:
<?php
$cardInfo = obfuscateCard('4356123445678900');
?>
This will return an array something like this:
Array (
[card_type] => Visa
[card_num] => **********678900
)
By default this function will replace the leftmost 10 digits of the card number with a splat (*). You can change this behavior by calling it with the quantity of digits to replace. Try it like this:
<?php
$cardInfo = obfuscateCard('4356123445678900',6);
?>
This will return the following:
Array (
[card_type] => Visa
[card_num] => ******345678900
)
You can use this as a handy way to disguise credit card numbers, or any other string.
Here is the complete source of the function. Enjoy!
<?php
function obfuscateCard($cardnum,$qty = 10)
{
$first4 = substr($cardnum,0,4);
switch(TRUE)
{
case ((($first4 >= 3000) && ($first4 <= 3059)) ||
(($first4 >= 3600) && ($first4 <= 3699)) ||
(($first4 >= 3800) && ($first4 <= 3889))):
$_tmp['card_type'] = 'Diners Club';
break;
case ((($first4 >= 3400) && ($first4 <= 3499)) ||
(($first4 >= 3700) && ($first4 <= 3799))):
$_tmp['card_type'] = 'American Express';
break;
case ((($first4 >= 3088) && ($first4 <= 3094)) ||
(($first4 >= 3096) && ($first4 <= 3102)) ||
(($first4 >= 3112) && ($first4 <= 3120)) ||
(($first4 >= 3158) && ($first4 <= 3159)) ||
(($first4 >= 3337) && ($first4 <= 3349)) ||
(($first4 >= 3528) && ($first4 <= 3589))):
$_tmp['card_type'] = 'JCB';
break;
case (($first4 >= 3890) && ($first4 <= 3899)):
$_tmp['card_type'] = 'Carte Blanche';
break;
case (($first4 >= 4000) && ($first4 <= 4999)):
$_tmp['card_type'] = 'Visa';
break;
case (($first4 >= 5100) && ($first4 <= 5599)):
$_tmp['card_type'] = 'MasterCard';
break;
case "5610":
$_tmp['card_type'] = 'Australian BankCard';
break;
case "6011":
$_tmp['card_type'] = 'Discover/Novus';
break;
}
$var = substr_replace($cardnum,'',0,$qty);
$_tmp['card_number'] = str_pad($var,$qty,"*",STR_PAD_LEFT);
return $_tmp;
} ?>
J Cornelius is a software developer, Web developer, and Formula 1 fan in Atlanta GA. He has a strange affinity for odd numbers, european sports cars, thoughtful analogies, and is hopelessly addicted to chips & salsa. Read more
Was it good for you?
Post to Digg Post to del.icio.us Post to ma.gnolia Post to Furl Post to Mixx