[eluser]Unknown[/eluser]
I came up with a single regexp to mask all but the first and last four digits of a CC. This will also keep any non-numbers.
Code:
preg_replace('/(?!^.?)[0-9](?!(.){0,3}$)/', '*', '3456-7890-1234-5678')
Keeps the
FIRST CHARACTER, the
LAST FOUR CHARACTERS, and any
NON-NUMERIC CHARACTERS in-between. Masks (*) everything else.
Code:
"3456-7890-1234-5678" = "3***-****-****-5678"
"4567890123456789" = "4***********6789"
"4928-abcd9012-3456" = "4***-abcd****-3456"
"498291842" = "4****1842"
If the regular expression is a bit confusing,
(?!) is a "look-ahead not-equals", meaning
make sure this does NOT come before or next, but leave it alone.