HTML Email Address Obfuscation



Published: 2017-05-31 07:53:14 +0000
Categories: PHP,

Language

PHP

Description

Small class, intended to be used as a callback for preg_replace_callback to obfuscate email addresses. You can also simply pass a string in rather than being obliged to use preg_replace_callback

Supports a number of obfuscation modes

  • part - e.g. test@<Domain Hidden>
  • full - e.g. <Email Hidden>
  • none - e.g. test@example.com
  • htmlencode - Generates a javascript snippet to write the address in
    
    document.write('&#116;&#101;&#115;&#116;');
    document.write('@');
    document.write('&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;');
        

Although it's targetted with HTML output in mind, it can just as easily be used on plain text

Snippet

class EmailObfusCallback {

    /** Constructor
    *
    * @arg obfustype - one of "part","full","none","htmlencode"
    * @return void
    */
    function __construct($obfustype="full"){
        $this->obfustype = $obfustype;
    }

    /** Function to obscure email addresses in a HTML page
    * Intended to be callaed from preg_replace_callback
    *
    * @arg str - the regex match 
    * @return str
    */
    function obscureEmail($match){
            global $conf;
            $parts = explode('@',$match[0]);
            switch (strtolower($this->obfustype)){
                    case 'part':
                            $str = "{$parts[0]}@&lt;Domain Hidden&gt;";
                            break;
                    case 'full':
                            $str = "&lt;Email Hidden&gt;";
                            break;
                    case 'none':
                            $str = $match[0];
                            break;
                    default:
                            $u = '';
                            for ($i = 0; $i < strlen($parts[0]); $i++){
                                    $u .= '&#' . ord($parts[0][$i]) . ';';
                            }
                            $d = '';
                            for ($i = 0; $i < strlen($parts[1]); $i++){
                                    $d .= '&#' . ord($parts[1][$i]) . ';';
                            }
                            $str = "<script type='text/javascript'>\n".
                                    "document.write('$u');\n" .
                                    "document.write('@');\n\n" .
                                    "document.write('$d');" .
                                    "</script>";
            }
            return $str;
    }

}

Usage Example

$str = "Email me at test@example.com for more info";
$callback = new EmailObfusCallback('part');
$regex = "/(([A-Z0-9._%-\+]+)@([A-Z0-9_%-]+)\.([A-Z\.]{2,20}))/i";
echo preg_replace_callback($regex,array($callback, 'obscureEmail'),$str) . "\n";

$callback = new EmailObfusCallback('full');
echo preg_replace_callback($regex,array($callback, 'obscureEmail'),$str) . "\n";

$callback = new EmailObfusCallback('none');
echo preg_replace_callback($regex,array($callback, 'obscureEmail'),$str) . "\n";

$callback = new EmailObfusCallback('htmlencode');
echo preg_replace_callback($regex,array($callback, 'obscureEmail'),$str) . "\n";

License

GNU GPL V2

Keywords

email, obfuscation, hiding, anti-spam, address,

Latest Posts

Urldecode string (LUA)
FFMPEG Convert YUV444p to YUV420p (BASH)
Forcing FFMPEG to honour segment length in HLS Stream Creator (BASH)
Force CURL to place request to a specific IP (BASH)
Set a variable only if it is undefined (Javascript)
Ignore query string with Nginx caching proxy (NGinx)
Get breakdown of UK and non-UK votes for a Parliamentary Petition (Python)
Decompress Mozilla Firefox jsonlz4 bookmark backup files (Python)
Setting Git author details for a single repo (Git)
Checking Virtual Machine Resource Allocations with Libvirt (BASH)

Copyright © 2019 Ben Tasker | Sitemap | Privacy Policy
Available at snippets.bentasker.co.uk and snippets.6zdgh5a5e6zpchdz.onion