Skip to content

Instantly share code, notes, and snippets.

@aapis
Last active January 8, 2021 23:23
Show Gist options
  • Save aapis/ca12d093582cc5c8d78f318aea66ce9b to your computer and use it in GitHub Desktop.
Save aapis/ca12d093582cc5c8d78f318aea66ce9b to your computer and use it in GitHub Desktop.
Email obfuscator
<?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