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 ( or SpamSpan filter ( which use more effective methods such as javascript encryption and things like that. Those 2 modules also integrate with Email Field ( to make your life even easier.

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 = ‘[email protected]’;
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: