Menu Close

How to hide email address partially in PHP?

How to hide email address partially in PHP?

Let me share the details for hiding the email address partially using PHP codes. I have seen some websites, hide the email address like “robin****@gmail.com”. 

In my opinion, we can use this for reset password function, when users click the reset password link then show the partially hidden email address to them and send the reset password link to their mail id. 

Here I show you how to hide the email address partially using PHP with example. So you can easily integrate with your PHP website and applications.

There are two methods for hiding email address,

Method 1:

$email1 = "robinkumacodeamend@gmail.com";
function hideEmail1($email1){
	if(filter_var($email1, FILTER_VALIDATE_EMAIL)){
        list($first, $last) = explode('@', $email1);
        //split two parts like before @ and after @
        $first = str_replace(substr($first, '5'), str_repeat('*', strlen($first)-3), $first);
        //declare the first word length
        $last = explode('.', $last);
        $domain = str_replace(substr($last['0'], '1'), str_repeat('*', strlen($last['0'])-1), $last['0']);
        //declare the last word length
        $hideEmailAddress = $first.'@'.$domain.'.'.$last['1'];
        return $hideEmailAddress;
    }
} 
echo hideEmail1($email1); 

Output

Output: robin***************@g****.com

Method 2:

$email1 = "robinkumacodeamend@gmail.com";
function hideEmail($email1){
    $em   = explode("@",$email1);
    $name = implode(array_slice($em, 0, count($em)-1), '@');
    $len  = floor(strlen($name)/3);
    return substr($name,0, $len) . str_repeat('*', $len) . "@" . end($em);   
}
echo hideEmail($email1); 

Output

Output: robink******@gmail.com

Share your comments.

Posted in PHP

You can also read...