How to Partially hide or mask email addresses in PHP

How to Partially hide email address in PHP

In this post, we are going to discuss how we can hide or mask email addresses using PHP. We can see in many application some part of the email is hidden for security. Even if you visit your profile on some big website they keep it to masked and show it to you when you click on the show email id option just like a password.

As you have observed it on Facebook, Google, and any other website when you try to reset the password using the forgot password option then the will show you email address which is partially hidden, if that email does not belong to you then due to masking you can not see which email id it is. So in this article, we will explain how you can partially hide email addresses in the PHP.

We will try to explain the email masking with two examples you can use any in your project and also can change the output by making some changes in the function as per requirement. Let’s suppose you need only the first 3 characters to be masked or hide, You can make those changes in the function.

Checkout more articles on PHP

How to Partially Hide Phone in PHP?

How to use curl in PHP

How to check an array is multidimensional or not in PHP?

PHP Multidimensional Array Search by Key and Value with Examples

To hide some characters from the email address, we will create a PHP function and pass the email address as an argument. Here we will give you two different types of examples so you can use any of them as per your requirement.

Example 1

function maskEmailAddress($email)
    if(filter_var($email, FILTER_VALIDATE_EMAIL))
        list($first, $last) = explode('@', $email);
        $first = str_replace(substr($first, '3'), str_repeat('*', strlen($first)-3), $first);
        $last = explode('.', $last);
        $last_domain = str_replace(substr($last['0'], '1'), str_repeat('*', strlen($last['0'])-1), $last['0']);
        $hideEmailAddress = $first.'@'.$last_domain.'.'.$last['1'];
        return $hideEmailAddress;
$email = "[email protected]";
echo maskEmailAddress($email);



Let’s try to understand the above code.

  • First, we used the FILTER_VALIDATE_EMAIL filter to validate an email address.
  • After validation, we used the explode function to split an email into two parts
  • 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.

Example 2

function partiallyHideEmail($email)
    $em   = explode("@",$email);
    $name = implode(array_slice($em, 0, count($em)-1), '@');
    $len  = floor(strlen($name)/2);
    return substr($name,0, $len) . str_repeat('*', $len) . "@" . end($em);   
$email = '[email protected]';
echo partiallyHideEmail($email);




We have tried to explain the email masking with the help of two examples. You can directly use the above code snippet if you need to hide or mask the email addresses in your project. Hope you like the article, If you have any queries please comment below we would love to help you.

Spread the love