Mask credit card numbering

Asked

Viewed 1,302 times

2

I have the following numbering:

4716501731980995

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

  • There are 16 digits, from the fifth to the tenth second should become asterisks? That

  • misworded question. and the other parts? 4***** *0995 or 47 ******995 etc. etc....

  • That’s right, Ray, but I think some of you might have less right, which is in the case of Americanexpress

  • Leocaracciolo, can edit as you think it best to be more enlightened for others, at the moment what I could think was this.

  • vc q have to say the composition of the 4 parts, because the first and fourth were without padão, example, I can have 1 digit in the first part and 4 in the last, or 2 in the first and 3 in the last, 3 in the first and 2 in the last, 4 in the first and 1 in the last

  • No, think to me... "I need to split it four ways. Part 2/3", that means: 16 digits, / 4 = 4 digits per part - that is, the 2 and 3 part contains exact 4 digits, in this case, must be replaced, as the Rray posted.

  • No, the question then should be "I need to divide it into 4 parts" EQUAL. Only part 2/3 has 5 asteristicos separated by space followed by 6 asteristicos that contradicts everything ***** ******

Show 2 more comments

2 answers

8


The simplest option is to use the function substr_replace(), in that reply has more details.

Another alternative is to use the function str_split() to break the string into an array with four elements and contain the zero Indice with the function str_repeat() to generate the asterisks plus the three Indice 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];

Exit:

4716********0995

8

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

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

Upshot:

4716 **** **** 0995

Browser other questions tagged

You are not signed in. Login or sign up in order to post.