How can I convert username in email addresses into asterisks. The first and last letter in the username stay as it is and rest replaced with (*).
Example:
[email protected]
into
m****[email protected]
You can use the asterisk (*) as a wildcard in email addresses when defining routes and in file names. Wildcards can appear in the name or domain sections of an email address. The following are valid examples: *@*: Valid representation of all email addresses.
Here's how it works. Suppose somebody sends you an e-mail message that doesn't really require an answer. However, to be polite, you want to say “thank you.” Instead of putting “Thanks” in the body of your reply, type *Thanks in the subject field of your reply.
Answer: There is no way to use an email address with an ampersand, e.. g. one&[email protected], for the Sender Email Address in an Email Campaign email message.
You can do it using look arounds.
/(?!^).(?=[^@]+@)/
(?!^)
Negative look behind. Checks if the character is not preceded by start of string. This ensures that the first character is not selected.
.
Matches a single character.
(?=[^@]+@)
Positive look ahead. Ensures that the single character matched is followed by anything other than @
( ensured by [^@]
) and then a @
Regex Demo
Example
preg_replace("/(?!^).(?=[^@]+@)/", "*", "[email protected]")
=> m****[email protected]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With