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 wordsloading...
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
Nice!
Small correction, if I may. Since I got this:
PHP Warning: strlen() expects parameter 1 to be string, array given in ….php
on line 123
I changed:
return $arr[0][0] . str_repeat(“*”, strlen($arr[0]) – 2).$arr[0][strlen($arr) – 1]. “@” . $arr[1];
To:
return $arr[0][0] . str_repeat(“*”, strlen($arr[0]) – 2).$arr[0][strlen($arr[0]) – 1]. “@” . $arr[1];
Thank you. yes. you are right, should use count function to obtain the length of the array.