mailto: Email Link Obfuscation (Hiding)

After searching for a good email link obfuscation script that allows the address to be seen as normal text i came upon a webpage that had a good solution, so i did a little reverse engineering and created this PHP script to output the same code, enjoy!

<?php
/**
 * @author Josh Hartman
 * @copyright 2010
 */
 
function strToHex($string, $prepend=null)
{
    $hex='';
    for ($i=0; $i < strlen($string); $i++)
    {
        $hex .= $prepend.dechex(ord($string[$i]));
    }
    return $hex;
}
 
function createRandomPassword() {
    $chars = "abcdefghijkmnopqrstuvwxyz023456789";
    srand((double)microtime()*1000000);
    $i = 0;
    $pass = '' ;
    while ($i <= 7) {
        $num = rand() % 33;
        $tmp = substr($chars, $num, 1);
        $pass = $pass . $tmp;
        $i++;
    }
    return $pass;
}
 
function mailto_link($email){
    if(preg_match('/^[A-Z0-9._%-]+@(?:[A-Z0-9-]+\\.)+[A-Z]{2,4}$/i',$email)){
        $var1 = createRandomPassword();
        $var2 = createRandomPassword();
        $email = explode('@',$email);
        $user = strToHex($email[0],'%');
        $domain_array = array_reverse(explode('.',$email[1]));
        $domain = "'";
        foreach ($domain_array as $k=>$v){
            $domain_array[$k] = strToHex($v,'%');
        }
        $domain = "'".implode("','",$domain_array);
        $domain .= "'";
        $output = $var1."=['".$user."',[".$domain."].reverse().join('.')].join('@');".$var2."=unescape(".$var1.");document.write(".$var2.".link('mai'+'lto:'+".$var1."));";
        $output = "<script type=\"text/javascript\">eval(unescape(\"".strToHex($output,'%')."\"));</script>";
        echo $output;
    }else{
        return false; //email address is not valid
    }
}
 
mailto_link('username@domain.com'); //use this function wherever you want an email link
?>