Possible Duplicate:
regexp to partly hide email?
I’m currently writing a script that hides an email address (so test@domain.com becomes t~~~@~~~~~~.com).
Whilst following the instructions on this question – regexp to partly hide email? – I have managed to get it to display t~~~@domain.com, but am having trouble removing the “domain”.
Here is what I have so far (assume $row->email is “test@domain.com”):-
$string = preg_replace("(?<=.).(?=.*@)","~", $row->email);
$string = preg_replace("(?<=@).[a-zA-Z0-9]*","~", $string);
However, all it returns is t~~~@~omain.com
I’m baffled as to how to get the rest of the domain bit. Ideas?
IDEALLY if anybody can provide a solution so it becomes t~~~@d~~~~~~.com, that’d be super.
Cheers
It is ..
"@"and then;.(any character: i.e. the"d"in"domain") and then;That is, the first and only the first character after the
@was matched and replaced with~.The following
forces the character class to match to the first
"."(period, as in".com") or end-of-input.Note that the
.is moved inside of the(?<=@.)-look-behind clause which causes it to skip the first letter after the"@". I have also added a hyphen ("-") to the character class as they are valid (and not terribly uncommon) in domain names.In addition, not all email addresses are in the trivial
"a@b.c"form and Internationalized Domain Names (or IDN) can be represented locally in a non-punycode form when not transmitted (e.g. not used in a restricting context), but that is another topic. (It may be more appropriate to replace[a-zA-Z0-9-]*with[^.]*due to IDN without further specification.)