Partially hides email address with PHP regex
I found two threads that at first seem to answer my query, but they only seem to be part of the solution to me. Using them, I got to where I am. So this is not a duplicate!
I want to replace everything except the first character in the name and domain part of an email address with an asterisk:
eg
g ****** @g ****. com or g ****** @g ****. co.uk
code:
$email2 = preg_replace('/(?<=.).(?=.*@)/', '*', $email);
$email3 = preg_replace('/(?<=@.)[a-zA-Z0-9-]*(?=(?:[.]|$))/', '*', $email2);
which almost exists, but gives me g ****** @g * .com , not g ****** @g *****. com
Can anyone help me with regex?
You can use:
$email = preg_replace('/(?:^|@).\K|\.[^@]*$(*SKIP)(*F)|.(?=.*?\.)/', '*', $email);
Demo version of RegEx
This will turn great@gmail.com
into g*****@g*****.com
and
myemail@gmail.co.uk
will become m*******@g*****.co.uk
and test.test@gmail.com
int*********@g*****.com
^.\K|.(?=.*@)|@.\K|\..*(*SKIP)(*F)|.(?=.*\.)
You can try this.Replace by. *
View demo.
https://regex101.com/r/mT0iE7/20