Níže uvedený skript nabízí základní ochranu e-mailu zveřejněném na webu proti spam robotům. Převádí text e-mailové adresy na HTML entity a zobrazuje jej pomocí JavaScriptu.
<?php
// javascript antispam e-mail link
function antispamlink($email, $title){
$return = "<script type=\"text/javascript\"><!--\ndocument.write('<a href=\"' + 'mailto:' + '";
$offset = 0;
while ($offset >= 0) {
$code1 .= '&#'.ordutf8($email, $offset).';';
}
$offset = 0;
while ($offset >= 0) {
$code2 .= '&#'.ordutf8($title, $offset).';';
}
$return .= $code1."' + '\">' + '".$code2."' + '<\/a>');\n--></script>";
return $return;
}
// utf-8 characters to ASCII (ord function isn't UTF-8 fiendly)
function ordutf8($string, &$offset) {
$code = ord(substr($string, $offset,1));
if ($code >= 128) { //otherwise 0xxxxxxx
if ($code < 224) $bytesnumber = 2; //110xxxxx
else if ($code < 240) $bytesnumber = 3; //1110xxxx
else if ($code < 248) $bytesnumber = 4; //11110xxx
$codetemp = $code – 192 – ($bytesnumber > 2 ? 32 : 0) – ($bytesnumber > 3 ? 16 : 0);
for ($i = 2; $i <= $bytesnumber; $i++) {
$offset ++;
$code2 = ord(substr($string, $offset, 1)) – 128; //10xxxxxx
$codetemp = $codetemp*64 + $code2;
}
$code = $codetemp;
}
$offset += 1;
if ($offset >= strlen($string)) $offset = -1;
return $code;
}
// print e-mail with title
echo antispamlink("info@dhosting.cz","Bc. Tomáš Daněk");
?>