<?php
/* Here's the logic: We want to show X numbers. If length of STR is less than X, hide all. Else replace the rest with *. */
function mask($str, $first, $last) { $len = strlen($str); $toShow = $first + $last; return substr($str, 0, $len <= $toShow ? 0 : $first).str_repeat("*", $len - ($len <= $toShow ? 0 : $toShow)).substr($str, $len - $last, $len <= $toShow ? 0 : $last);
}
function mask_email($email) { $mail_parts = explode("@", $email); $domain_parts = explode('.', $mail_parts[1]);
$mail_parts[0] = mask($mail_parts[0], 2, 1); // show first 2 letters and last 1 letter $domain_parts[0] = mask($domain_parts[0], 2, 1); // same here $mail_parts[1] = implode('.', $domain_parts);
return implode("@", $mail_parts);
}
$emails = array( 'a@a.com', 'ab@aa.com', 'abc@aaa.com', 'abcd@aaaa.com', 'abcde@aaaaa.com', 'abcdef@aaaaaa.com', 'abcdefg@aaaaaaa.com', 'abcdefgh@aaaaaaaa.com', 'abcdefghi@aaaaaaaaa.com'
);
foreach ($emails as $email){ echo '<b>'.$email.'</b><br>'.mask_email($email).'<br><hr>';
}