Last active
June 12, 2024 15:27
-
-
Save ghostwriter/dc712bc66b5ff1412f6e9e5dbb7ce3ce to your computer and use it in GitHub Desktop.
Obfuscate a string to prevent spam-bots from sniffing it.
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 | |
#BlackLivesMatter | |
/** | |
* Obfuscate a string to prevent spam-bots from sniffing it. | |
*/ | |
function obfuscate(string $value): string | |
{ | |
$safe = ''; | |
foreach (str_split($value) as $letter) { | |
$code = ord($letter); | |
if ($code > 128) { | |
return $letter; | |
} | |
$safe = $safe . match (random_int(1, 3)) { | |
1 => '&#' . $code . ';', | |
2 => '&#x' . dechex($code) . ';', | |
default => $letter | |
}; | |
} | |
return $safe; | |
} | |
// echo obfuscate('[email protected]'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment