Masking credit card numbering

2

I have the following numbering:

4716501731980995

I need to split it into 4 parts. Part 2/3 should be replaced by **** **** How could I do this?

    
asked by anonymous 01.08.2017 / 22:35

2 answers

7

The simplest option is to use the substr_replace() function, this answer has more details.

Another alternative is to use the str_split() function to break the string into an array with four elements and contain the zero index with the function str_repeat() to generate the asterisks and more index three containing the last part of the card.

Example - ideone - str_split

$str = '4716501731980995';
$novo = str_split($str, 4);
echo $novo[0]. str_repeat('*', 8). $novo[3];

Output:

4716********0995
    
01.08.2017 / 22:45
8

Another option would be to use substr_replace() , since the string is not multibyte, it will not be a problem . This will not break / split the string, just insert the asterisks in the defined spaces.

$string = '4716501731980995';
echo substr_replace($string, ' **** **** ', 4, 8);

Result:

4716 **** **** 0995
    
01.08.2017 / 23:16