PHP Zamaskovanie email adries
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";
}
Vystup:
"Abc\*****@iana.org
abcdl*****@hotmail.com
Metoda2:
<?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>';
}
vystup:
a@a.com
*@*.com
ab@aa.com
**@**.com
abc@aaa.com
***@***.com
abcd@aaaa.com
ab*d@aa*a.com
abcde@aaaaa.com
ab**e@aa**a.com
abcdef@aaaaaa.com
ab***f@aa***a.com
abcdefg@aaaaaaa.com
ab****g@aa****a.com
abcdefgh@aaaaaaaa.com
ab*****h@aa*****a.com
abcdefghi@aaaaaaaaa.com
ab******i@aa******a.com
Zdroj:
https://stackoverflow.com/questions/20545301/partially-hide-email-address-in-php