Replacing characters in email address with *

I want to display email addresses on a public page with all but the first character of the name part and all but the first character of the domain replaced by asterisks eg g*****@g****.com - however I can’t get my brain around the logic to do this. Can anyone help please?

This answers it better than I could. He explains the logic behind how to break apart the problem as well.

http://stackoverflow.com/questions/12253740/partially-hide-email-addresses-with-php-regular-expression

Thanks. I have trouble getting my head around complex regular expressions. I have used

$email = preg_replace('/(?<=@.)[a-zA-Z0-9-]*(?=(?:[.]|$))/', '*', $email2);

but this replaces all the characters between @ and . with a single asterisk. g*.com instead of g****.com (for gmail.com).

Using

$email = preg_replace('/(?<=.).(?=.*@)/', '*', $email2);

seems to do the job for the name part…

Sorry, I’m not sure my previous reply was very clear! I now have

$email2 = preg_replace('/(?<=.).(?=.*@)/', '*', $email);
$email3 = preg_replace('/(?<=@.)[a-zA-Z0-9-]*(?=(?:[.]|$))/', '*', $email2);

which gives me g******@g*.com instead of gandalf@gmail.com. Any ideas how I change the 2nd regex to give me g*****@g****.com ? Thanks!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.