Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mask credit card number in PHP

Tags:

php

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.

like image 882
fawad Avatar asked Nov 16 '12 09:11

fawad


People also ask

What is masked credit card number?

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.

What is a masked or Virtual credit card?

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.

How do I find my virtual credit card number?

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.”

How do you mask card numbers in Python?

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='*' ).


2 Answers

You can use substr_replace

$var = '1234123412341234';
$var = substr_replace($var, str_repeat("X", 8), 4, 8);
echo $var;

Output

1234XXXXXXXX1234
like image 108
Baba Avatar answered Oct 14 '22 07:10

Baba


This should work using substr:

function ccMasking($number, $maskingCharacter = 'X') {
    return substr($number, 0, 4) . str_repeat($maskingCharacter, strlen($number) - 8) . substr($number, -4);
}
like image 25
h2ooooooo Avatar answered Oct 14 '22 07:10

h2ooooooo