Aidan's PHP Repository

A repository for PHP functions and classes ...

function.mail_obfuscate.php

Obfuscate an email address

  • Author: Aidan Lister <aidan@php.net>
  • Version: 1.1.0
  • Link: http://aidanlister.com/repos/v/function.mail_obfuscate.php
  • Views: 7528
  • Downloads: 1497

Source

Download this script <?php
/**
 * Obfuscate an email address
 *
 * @author      Aidan Lister <aidan@php.net>
 * @version     1.1.0
 * @link        http://aidanlister.com/repos/v/function.mail_obfuscate.php
 * @param       string      $email      E-mail
 * @param       string      $text       Text
 */
function mail_obfuscate($email, $text = '')
{
    // Default text
    if (empty($text)) {
        $text = $email;
    }
 
    // Create string
    $string = sprintf('document.write(\'<a href="mailto:%s">%s</a>\');',
            htmlspecialchars($email),
            htmlspecialchars($text));
 
    // Encode    
    for ($encode = '', $i = 0; $i < strlen($string); $i++) {
        $encode .= '%' . bin2hex($string[$i]);
    }
 
    // Javascript
    $javascript = '<script language="javascript">eval(unescape(\'' . $encode . '\'))</script>';
 
    return $javascript;
}
 
?>

Comments

August 12th, 2006
Excelent function! thank you for sharing your code :)
February 4th, 2006
GOOD WORK MEN!. NICE NICE NICE with this modification you can set class attributes or id for CSS support. old: function mail_obfuscate($email, $text = '') new: function mail_obfuscate($email, $text = '', $plus_params='') old: $string = sprintf('document.write(\'%s\');', htmlspecialchars($email), htmlspecialchars($text) new: $string = sprintf('document.write(\'%s\');', htmlspecialchars($email), $plus_params, htmlspecialchars($text) usage: mail_obfuscate("info@riaevolution.com", $text = 'info@riaevolution.com', 'class="link_content_footer"');
December 9th, 2005
i love this one .... I HATE spammers ... :)
November 20th, 2005
This is losely based on the obfuscator in Smarty, and is the function used to obfuscate the emails in the comments on this site.
October 26th, 2005
very nice;) thnx a lot for this useful code