Protect your email address from spam bots

Adding your email address to one of your web pages makes it easy for spam bots to pick up that address and, well, spam you. In this code snippet we'll look at converting each character to its ASCII equivalent so that bots can't read it, but to your visitors it looks exactly the same.

How do spam bots work?

Spam bots crawl the web in a way that is similar to how Google crawls your pages to collect data for search results. The difference is that spam bots are looking for very specific information like forms they can post to and email addresses to collect for spam mailing lists. 

How to hide the email address from spam bots

When a spam bot crawls your page, it isn't reading it in a way a human would, it's looking at your site's source code. Characters on a page can be represented in different ways, even though it will ultimately look the same to the site visitors, we can utilise this to make it unreadable for the spam bots but still appear fine for everyone else. To achieve this we're going to convert every character to it's equivilent ASCII character in a foreach loop:

function hideEmail($email)
    foreach(str_split($email, 1) as $character)
        echo '&#' . ord($character) . ';';

So this works by using PHP's str_split function to break the string into an array that it can then loop through. Each loop applies the ord() function to convert each character to echo out the ASCII equivilent.

This function can now be called like so:


To your readers this will look the same as normal, but to spam bots reading the source code it will look like this:


Check that yours is working by right clicking in your browser and going to 'View Source'.


If you're using wordpress there are built in functions to take care of this for you, check out PaulUnd's post on using the antispambot() function.

Sign Up
comments powered by Disqus

Popular Tags

Need a web developer?

If you'd like to work with code synthesis on your next project get in touch via the contact page.