Encode your email links to prevent spam

One of the popular ways to hide your email on a web page from spam bots is to display the email as an image or to use the ‘[at]’ word instead of the ‘@’ sign. The code given here is yet another way to fight spam. The below function will let you to encode email or other links to their equivalent HTML entity encoded syntax. This will enable you to hide your web-page emails from spam bots. As the browser converts and displays the appropriate string from the encoding the user will be able to correctly see the email id, but a spam bot will have a difficult time to decode the encoded string. Of course we now have quite sophisticated crawlers that can work around this types of encoding, but for other crawlers that rely on regular expressions or other such simple methods, they will find it difficult to grab the email links from the page.

Encoding HTML characters

The function is quite simple. It encodes email, links or other string characters to their ‘Numeric character references’. For example ‘code‘ will be encoded to code – a decimal encoding or code – a hexadecimal encoding.
The function randomly encodes a character to a hex or decimal representation, so each page refresh will encode the same string to a different format.

<?php
 
function encodeString($email)
{
    $enc_email = '';
    srand();
 
    # Convert each character to decimal or hex representation
    for ($i=0; $i < strlen($email); $i++) {
 
        if(rand(0,1) == 0) {
            $enc_email .= "&#" . (ord($email[$i])) . ";";
        } else {
            $enc_email .= "&#X" . dechex(ord($email[$i])) . ";";
        }
    }
 
    return $enc_email;
}
 
echo encodeString("designhouse@host.com");
 
?>

This will convert the email ‘designhouse@host.com’ to

&#100;&#101;&#X73;&#X69;&#X67;&#X6e;&#104;
&#X6f;&#X75;&#115;&#101;&#64;&#X68;&#111;
&#X73;&#116;&#X2e;&#99;&#111;&#109;

As it uses a random base (decimal or hex), the actual results may differ from yours.

3 thoughts on “Encode your email links to prevent spam

Comments are closed.