Mask Credit Card Number in PHP

Mask credit card number in PHP

This should work using substr:

function ccMasking($number, $maskingCharacter = 'X') {
return substr($number, 0, 4) . str_repeat($maskingCharacter, strlen($number) - 8) . substr($number, -4);
}

Masking credit card number

Regular expressions will work fine, so what you have is good.

To actually do the regex, use this code:

$str = "The quick brown fox jumps over 5192696222257727 dog.";

$masked = preg_replace("/(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})/", "XXXXXXXX", $str);

Then you can do whatever with $masked.

EDIT: Fixed the regex, yours only matches it if the entire string is the number.

masking credit card & bank account information

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

How to mask first 6 and last 4 digits for a credit card number in .net

Try Regex: (?<=\d{4}\d{2})\d{2}\d{4}(?=\d{4})|(?<=\d{4}( |-)\d{2})\d{2}\1\d{4}(?=\1\d{4})

Regex Demo

C# Demo

Explanation:

2 alternative regexes
(?<=\d{4}\d{2})\d{2}\d{4}(?=\d{4}) - to handle cardnumbers without any separators (- or <space>)
(?<=\d{4}( |-)\d{2})\d{2}\1\d{4}(?=\1\d{4}) - to handle cardnumbers with separator (- or <space>)

1st Alternative (?<=\d{4}\d{2})\d{2}\d{4}(?=\d{4})
Positive Lookbehind (?<=\d{4}\d{2}) - matches text that has 6 digits immediately behind it
\d{2} matches a digit (equal to [0-9])
{2} Quantifier — Matches exactly 2 times
\d{4} matches a digit (equal to [0-9])
{4} Quantifier — Matches exactly 4 times
Positive Lookahead (?=\d{4}) - matches text that is followed immediately by 4 digits
Assert that the Regex below matches
\d{4} matches a digit (equal to [0-9])
{4} Quantifier — Matches exactly 4 times

2nd Alternative (?<=\d{4}( |-)\d{2})\d{2}\1\d{4}(?=\1\d{4})

Positive Lookbehind (?<=\d{4}( |-)\d{2}) - matches text that has (4 digits followed by a separator followed by 2 digits) immediately behind it
1st Capturing Group ( |-) - get the separator as a capturing group, this is to check the next occurence of the separator using \1
\1 matches the same text as most recently matched by the 1st capturing group (separator, in this case)
Positive Lookahead (?=\1\d{4}) - matches text that is followed by separator and 4 digits

Masking credit card number using regex

You can use this regex with a lookahead and lookbehind:

str = str.replaceAll("(?<!^..).(?=.{3})", "*");
//=> **0**********351

RegEx Demo

RegEx Details:

  • (?<!^..): Negative lookahead to assert that we don't have 2 characters after start behind us (to exclude 3rd character from matching)
  • .: Match a character
  • (?=.{3}): Positive lookahead to assert that we have at least 3 characters ahead

CakePHP Validate Masked Credit Card With Regex

Not a complete answer but in your first example, a period inside [ ] actually matches the character . not anything. So if you change that one to:

/^.+$/

it will probably work better.

In the second example you should use curly braces { } instead of parenthesis to designate length. Like so:

/^[X]+[0-9]{4,}$/

The last one looks good though. But by fixing the first two you might get started on finding the source of the problem at least!



Related Topics



Leave a reply



Submit