note 98585 modified in function.preg-replace by danbrown
| From: | danbrown@php.net | Date: | Fri, 25 Jun 2010 13:33:09 +0000 |
| Subject: | note 98585 modified in function.preg-replace by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-170413@lists.php.net to get a copy of this message | ||
To mask credit cards using a single regexp expression (keeping the first and last four digits):
preg_replace('/(?!^.?)[0-9](?!(.){0,3}$)/', '*',
'3456-7890-1234-5678')
Keep the FIRST CHARACTER, the LAST FOUR CHARACTER, and any NON-NUMERIC CHARACTERS in-between. Mask
(*) everything else.
"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.
--was--
[MINOR EDIT TO MY PREVIOUS POST]
To mask credit cards using a single regexp expression (keeping the first and last four digits):
preg_replace('/(?!^.?)[0-9](?!(.){0,3}$)/', '*',
'3456-7890-1234-5678')
Keep the FIRST CHARACTER, the LAST FOUR CHARACTER, and any NON-NUMERIC CHARACTERS in-between. Mask
(*) everything else.
"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.
http://php.net/manual/en/function.preg-replace.php