Tuesday, December 20, 2005

PHP Email Encode

Function to encode an email address to help protect against spamming.

function mailto_link($email)
{
 $encoded = bin2hex($email);
 $encoded = chunk_split($encoded, 2, '%');
 $encoded = '%'.substr($encoded, 0, strlen($encoded) - 1);
 $htmlencoded = substr(chunk_split(bin2hex(" $email"), 2, ";&#x"), 3,-3);
 return "<a href=\"mailto:$encoded\">$htmlencoded</a>";
}

Takes an email address as the parameter and outputs an encoded mailto link. You could use this while looping through email records in a database for example.

<?php echo mailto_link("user@example.com"); ?>

Tags: ,

No comments: