There are thousands of ways to protect your email from being read by a spam bot. Complex ones uses images or Flash to display email as graphics, other uses JavaScript to dynamically create email anchors. I always try to use simple and still powerful methods, which both work as expected and are easy to implement, today I’m going to show email protection in my way.
For email addresses hiding I use simple PHP function, which takes email address as a string and HEX-encodes it. This function can also create whole anchor tag for you if you specify text for anchor (<a>TEXT</a>). Code looks like this (main part from Smarty plugin “mailto”):
function getMailToEncoded ($address, $text = null) { $address_prepend = 'mailto:'; $address_encode = ''; for ($x = 0; $x < strlen($address_prepend); $x++) { $address_encode .= '' . bin2hex($address_prepend[$x]).';'; } for ($x = 0; $x < strlen($address); $x++) { if(preg_match('!\w!',$address[$x])) { $address_encode .= '%' . bin2hex($address[$x]); } else { $address_encode .= $address[$x]; } } if (!$text) return $address_encode; $text_encode = ''; for ($x = 0; $x < strlen($text); $x++) { $text_encode .= '' . bin2hex($text[$x]).';'; } return '<a href="'.$address_encode.'">'.$text_encode.'</a>'; } echo getMailToEncoded ('juozas@juokaz.com', 'email me');
Output will look like this: email me, but actual HTML code has been transformed to:
<a href=”mailto:%6a%75%6f%7a%61%73@%6a%75%6f%6b%61%7a.%63%6f%6d”>email me</a>
Nasty! Bots are getting smarter and smarter, so after a while they will understand this code, but for initial protection this code works well. If you want additional protection, you can easily modify this function to put output in javascript block, where hex-encoded string is outputted by unescape() and then eval() functions. Try it!







