note 98583 deleted from function.preg-replace by danbrown
| From: | danbrown@php.net | Date: | Fri, 25 Jun 2010 13:34:05 +0000 |
| Subject: | note 98583 deleted from function.preg-replace by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-170415@lists.php.net to get a copy of this message | ||
Note Submitter: matt at mattjanssen dot com
----
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 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.