在某个网站上看到一串 email 的混淆字符串,通常,这些地址会被标记为'[email protected]'或类似的占位符。搜了一下,竟然是 cloudflare 家的。
混淆
php
private function encodeEmail($email, $key=0)
{
$chars = str_split($email);
$string = '';
$key = $key ? $key : rand(10, 99);
foreach ($chars as $value) {
$string .= sprintf("%02s", dechex(ord($value)^$key));
}
return dechex($key).$string;
}
还原
php
private function decodeEmail($encode)
{
$k = hexdec(substr($encode,0,2));
for($i=2, $m=''; $i < strlen($encode) - 1; $i += 2){
$m.=chr(hexdec(substr($encode, $i, 2))^$k);
}
return $m;
}