Skip to content

Instantly share code, notes, and snippets.

@ghostwriter
Last active June 12, 2024 15:27
Show Gist options
  • Save ghostwriter/dc712bc66b5ff1412f6e9e5dbb7ce3ce to your computer and use it in GitHub Desktop.
Save ghostwriter/dc712bc66b5ff1412f6e9e5dbb7ce3ce to your computer and use it in GitHub Desktop.
Obfuscate a string to prevent spam-bots from sniffing it.
<?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('oss@bmhv.org');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment