Hide email addresses from spambots

This function takes any string and converts it into an ASCII encoded character. I wrote this function recently to help prevent email harvesting spambots from collecting email addresses from a client's site.

This method is far from robust, and will only deter fairly rudimentary spambots from collecting email addresses from your site. If you are using a CMS like Drupal, I would highly recommend using a module such as Invisimail (http://drupal.org/project/invisimail) or SpamSpan filter (http://drupal.org/project/spamspan) which use more effective methods such as javascript encryption and things like that. Those 2 modules also integrate with Email Field (http://drupal.org/project/email) to make your life even easier.

<?php
function encode_ascii($email) {
  $num = strlen($email);
  for ($i = 0; $i < $num; $i++) {
    $char = substr($email, $i, 1);
    $encoded[] = '&#'. ord($char) .';'; 
  }
  return implode($encoded);
}
 
// to use this function, simple pass your email address string into it.
$email = 'fake@email.com';
print 'Email Address: '. encode_ascii($email);
 
?>

The result should not be obvious (because your browser should automatically handle decoding the ASCII characters), but if you look at the source of the page, you should see something like this:

Email Address: &#102;&#97;&#107;&#101;&#64;&#101;&#109;&#97;&#105;&#108;&#46;&#99;&#111;&#109;

Enjoy!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <drupal5>, <drupal6>, <javascript>, <php>. The supported tag styles are: <foo>, [foo].
  • Allowed HTML tags: <p> <a> <em> <strong> <cite> <pre> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.