How to Mask Email Addresses using PHP Function?


For users privacy we don’t want to display the full email addresses – and we can use the following PHP function to mask the email addresses properly. For example: [email protected] will be masked into a***[email protected]

Here we only mask the email account name and leave the account domain names intact.

1
2
3
4
5
<?php
function maskEmail($x) {
    $arr = explode("@", trim($x));
    return $arr[0][0] . str_repeat("*", strlen($arr[0])  - 2).$arr[0][count($arr) - 1]. "@" . $arr[1];
}
<?php
function maskEmail($x) {
    $arr = explode("@", trim($x));
    return $arr[0][0] . str_repeat("*", strlen($arr[0])  - 2).$arr[0][count($arr) - 1]. "@" . $arr[1];
}

Assuming the email address given is valid. We should use count to return the length of the array rather than use strlen which works for a string type.

See also: How to Filter the Unique Email Addresses?

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
192 words
Last Post: Teaching Kids Programming - Dynamic Programming or Greedy Algorithm to Compute the Min Change
Next Post: Teaching Kids Programming - Two Pointer Algorithm to Solve Four Sum Problem

The Permanent URL is: How to Mask Email Addresses using PHP Function?

2 Comments

  1. Miller

Leave a Reply