posted by kevin on April 26, 2009

The Problem

People like to display their email addresses on the page so people can email them directly. However there are bots that will 'scrape' your webpages and any visible email addresses will be scraped, sold, and spammed.

The solution

Don't display a typical email address format, but instead display the email address in a mix of ASCII values for characters and then some characters mixed in there as well.

View the source on the link below:

korb.kevin@gmail.com

You'll notice that it looks rather odd since the o is replaced with o and the r is replaced with a r etc.

 
<?php
class CA_Filter {
public static function obfuscate_email($email) {
    $encrypted_email = '';
    $len = strlen($email);
    for ($i=0;$i<$len;$i++) {
        if($i%3 == 0) {
            $encrypted_email .= $email[$i];
        }
        else {
            $encrypted_email .= "&#" . ord($email[$i]).";";
        }
 
    }
    return $encrypted_email;
}
 
/**
 * Obfuscate Email Link
 * 
 * This works to translate an email address into a web-friendly bot-unfriendly
 * address.
 *
 * @param string $email
 * @return string
 */
public static function obfuscate_email_link($email) {
    $encrypted_email = self::obfuscate_email($email);
    return "<a href=\"mailto:$encrypted_email\" target=_blank>$encrypted_email</a>";
}
}
?>
 

So to simply output your email address as a clickable link in a format that is less likely to be scraped you would use the static method:

 
<?php
echo CA_Filter::obfuscate_email_link("username@example.com");
?>
 
Bookmark and Share

Leave a Comment

Your email address will not be published.

(You can enclose code in <php></php> blocks.)

You may use Markdown syntax.

Please enter the letters as they are shown in the image above.
Letters are not case-sensitive.