Last active
January 8, 2021 23:23
-
-
Save aapis/ca12d093582cc5c8d78f318aea66ce9b to your computer and use it in GitHub Desktop.
Email obfuscator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$email = "[email protected]"; | |
$expectedResult = "ma*****@co***********.com"; | |
$result = obfuscator($email); | |
if ($result == $expectedResult) { | |
echo "You're great and smart".PHP_EOL; | |
exit(0); | |
} else { | |
echo "You done goofed".PHP_EOL; | |
exit(1); | |
} | |
/** | |
* Obfuscates email addresses | |
* [email protected] == ry**@ye**********.com | |
* | |
* @param string $email | |
* | |
* @return string | |
*/ | |
function obfuscator(string $email) | |
{ | |
$out = []; | |
foreach (explode("@", $email) as $str) { | |
array_push( | |
$out, | |
preg_replace_callback( | |
'/(?<=^.{2})[^.]*/', | |
function ($m) { | |
return str_repeat('*', strlen($m[0])); | |
}, | |
$str | |
) | |
); | |
} | |
return implode('@', $out); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment