I'm looking for a php function which can mask credit card & bank information such as routing number and account numbers. I need to mask many formats, so the existing stack overflow answers don't help me that much.
So for example, if the input is 304-443-2456
, the function should return xxx-xxx-2456
.
Sometimes the number has dashes, and can be in various lengths.
I'm looking for something generic, that I can extend as needed, preferably a zend framework view helper class.
When you use a masked credit card number, you get a new, unique credit card number with an expiration date and security code. This new number gets linked to your normal credit card account, but the seller never sees your real card details.
Use an online payment service. Purchases you make through online payment services, such as PayPal or Google Pay will show up on your bank or credit card statement as the name of the payment service. This can help mask your surprise purchase by hiding the retailer you purchased from.
PAN masking hides a portion of the long card number, or PAN, on a credit or debit card, protecting the card account numbers when displayed or printed. DTMF masking is used in specific technology when a customer enters their card details over the phone.
Some little regex in a function of it own, configuration available:
$number = '304-443-2456';
function mask_number($number, $count = 4, $seperators = '-')
{
$masked = preg_replace('/\d/', 'x', $number);
$last = preg_match(sprintf('/([%s]?\d){%d}$/', preg_quote($seperators), $count), $number, $matches);
if ($last) {
list($clean) = $matches;
$masked = substr($masked, 0, -strlen($clean)) . $clean;
}
return $masked;
}
echo mask_number($number); # xxx-xxx-2456
If the function fails, it will return all masked (e.g. a different seperator, less than 4 digits etc.). Some child-safety build in you could say.
Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With