Partially hide email address in PHP
You may have noticed that sometimes email addresses are partially hidden i.e. `your******@gmail.com` on google, social sites for verification and other security purposes. Today we’ll show you how to partially hide email address in PHP.
Hide email address with stars, how to partially mask/hide email address using PHP, Hide part of email address in php, Hide email address in PHP with asterisk (*), PHP to partially hide part of an email, mask email address, Replace some characters with Asterisks in PHP, Replacing characters in email address with *, Star-Hide Email, PHP hide_email()
Checkout more articles on PHP
- Ajax POST request with JQuery and PHP
- Convert base64 to image file in PHP
- Convert an image to base64 encoded string in PHP
- Autocomplete Textbox Using PHP, MySQL and jQuery
- Remove the last character from a string in PHP
To hide some characters from the email address, we will create a php function and pass the email address as argument. Here I will give you two different types of examples so you can use any of them as per your requirement.
Way to partially hide email address in PHP
1. Partially hide first part of the email address
Use the following code to partially hide the first part of an email address.
<!--?php <p-->
function partiallyHideEmail($email)
{
// use FILTER_VALIDATE_EMAIL filter to validate an email address
if(filter_var($email, FILTER_VALIDATE_EMAIL))
{
// split an email by "@"
list($first, $last) = explode("@", $email);
// get half the length of the first part
$len = floor(strlen($first)/2);
// partially hide a string by "*" and return full string
return substr($first, 0, $len) . str_repeat('*', $len) . "@" . $last;
}
}
echo partiallyHideEmail("[email protected]"); // Output: con***@cluemediator.com
echo partiallyHideEmail("[email protected]"); // Output: your****@gmail.com
?>
Let’s try to understand the above code.
- First we used the `FILTER_VALIDATE_EMAIL` filter to validate an email address.
- After successful validation, we used the explode function to split an email in two parts by using the `@` character.
- Now we used the strlen function to get half the length of the first part.
- At last we used the substr and str_repeat function to partially hide a string by `*` character and return the full value.
2. Partially hide the full email address
Now in this step we’ll show you how to partially hide the full email address i.e. `con****@clueme******.com`.
<!--?php <p-->
function partiallyHideEmail($email)
{
// use FILTER_VALIDATE_EMAIL filter to validates an email address
if(filter_var($email, FILTER_VALIDATE_EMAIL))
{
// split an email by "@"
list($first, $last) = explode('@', $email);
// get half the length of the first part
$firstLen = floor(strlen($first)/2);
// partially hide a first part
$first = str_replace(substr($first, $firstLen), str_repeat('*', strlen($first) - $firstLen), $first);
// get the starting position of the "."
$lastIndex = strpos($last, ".");
// divide last part in two different strings
$last1 = substr($last, 0, $lastIndex);
$last2 = substr($last, $lastIndex);
// get half the length of the "$last1"
$lastLen = floor(strlen($last1)/2);
// partially hide a string by "*"
$last1 = str_replace(substr($last1, $lastLen), str_repeat('*', strlen($last1) - $lastLen), $last1);
// combine all parts together and return partially hide email
$partiallyHideEmail = $first.'@'.$last1.''.$last2;
return $partiallyHideEmail;
}
}
echo partiallyHideEmail("[email protected]"); // Output: con****@clueme******.com
echo partiallyHideEmail("[email protected]"); // Output: your*****@gm***.com
echo partiallyHideEmail("[email protected]"); // Output: anothe******@te**.co.uk
?>
Understand the code line by line.
- Same as the above function we have divided an email address in two parts and partially hide the first part of an email address.
- Now in the next step we have divided the second part of an email address with the first `.` character by using the substr function.
- Here we only partially hide the first part that we get by the above point.
- Finally now returns the partially hidden email by combining all the parts.
That’s it for today.
Thank you for reading. Happy Coding..!!