PHP Forums Archive

HTML Hide mailto URL

Tags: encoding, e-mail conversion, unicode, php function, html tag

Olaf posted on 2010-12-27 20:15:38 #

Use this function to hide an e-mail address from spam-bots on your website. This custom PHP function will convert the e-mail address into Unicode values and will add the html tag and optional extra e-mail info like subject and body text. Test the demo page for an example.

function hide_mailto($mail, $label, $subject = "", $body = "") {
    $chars = preg_split("//", $mail, -1, PREG_SPLIT_NO_EMPTY);
    $new_mail = "<a href=\"mailto:";
    foreach ($chars as $val) {
        $new_mail .= "&#".ord($val).";";
    }
    $new_mail .= ($subject != "" && $body != "") ? "?subject=".$subject."&body=".$body : "";
    $new_mail .= "\">".$label."";
    return $new_mail;
}

Example usage:

echo hide_mailto('info@domain.com', 'Open a link', 'The email subject', 'The mail message...');

Comments / discussions

Olaf posted on 2010-12-27 20:21:11 #

Use this Javascript variant if you don't like to use a PHP function:

function create_mail(naam, domain, tld, subject, mailtext, label) {
    var mail = "";
    mail += '<a href="'
    mail += 'ma';
    mail += 'il';
    mail += 'to:';
    mail += naam;
    mail += '@';
    mail += domain;
    mail += '.';
    mail += tld;
    mail += '?subject=' + subject;
    mail += '&body=' + mailtext + '">';
    mail += label + '<';
    mail += '/a>';
    document.write(mail);
}