I have credit card number which I want to mask as below:
$cc = 1234123412341234
echo cc_masking($cc)
1234XXXXXXXX1234
function cc_masking($number) {.....}
Please suggest the regular expression for this.
A masked card is a digital service you use in combination with your normal card. When you mask your card, you get a new unique card number, expiration date and security code that you can use to make purchases. Those details lead to a dummy account that will, in turn, charge your real credit card or bank account.
A masked credit card is sometimes called a virtual credit card. When you mask your credit card, you get a new unique credit card number, expiration date and security code that you can use to make purchases without ever exposing your real credit card details.
Where and How to Get a Virtual Card. If you have a credit card, you may already have access to a virtual card number. To find out, simply log into your online bank or card account and search for “Virtual Card Number” or “Virtual Account Number.”
To define this program, we'll provide three inputs as the function's arguments: the credit card number as a string ( cc_string ), four digits in the credit card string ( digits_to_keep=4 ), and an asterisk to hide the character of each number in the credit card string ( mask_char='*' ).
You can use substr_replace
$var = '1234123412341234';
$var = substr_replace($var, str_repeat("X", 8), 4, 8);
echo $var;
Output
1234XXXXXXXX1234
This should work using substr
:
function ccMasking($number, $maskingCharacter = 'X') {
return substr($number, 0, 4) . str_repeat($maskingCharacter, strlen($number) - 8) . substr($number, -4);
}
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