Ad Code

Partially hide email address in PHP

I am building a simple friend/buddy system, and when someone tries to search for new friends, I want to show partially hidden email addresses, so as to give an idea about who the user might be, without revealing the actual details.

here's something quick:

function obfuscate_email($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);   
}

// to see in action:
$emails = ['"Abc\@def"@iana.org', 'abcdlkjlkjk@hotmail.com'];

foreach ($emails as $email){
    echo obfuscate_email($email) . "\n";
}


echo:

"Abc\*****@iana.org
abcdl*****@hotmail.com



Post a Comment

0 Comments