Image of the glider from the Game of Life by John Conway
Skip to content

Email Obfuscation- PHP Style

In my last post, I mentioned that I was picking up a new Perl project to obfuscate an email address from the traditional form to encoded ASCII. However, digging into the project reveals that everything can be handled with a simple PHP script. The code is so simple, actually, that little remains left to code. Here is what I have come up with so far:

< ?php
    $email= "webmaster@example.com";                // store the email address in a string variable
    $length = strlen($email);                         // get the length of the email address

    for ($i = 0; $i < $length; $i++)                  // work your way through each character of the email address
        $obfuscatedEmail .= "&#" . ord($email[$i]); // get the deicmal ASCII value of the character
?>

Fairly striaght forward, I think. Merely get the email address from a form on an HTML page, and store it in a variable called $email. Then, get the length of that email address and store that result in $length. Finally, using a for loop, step our way though the email address character by character until we have reached the email address character length, getting the ASCII value and appending "&#" to the beginning of each character. That result is stored in a variable called $obfuscatedEmail.

{ 6 } Comments

  1. AeroDesigns Web Designers using Internet Explorer 7.0 on Windows XP | April 6, 2007 at 4:40 am | Permalink

    Great code, just trying it out. Can't believe it's as simple as it seems. Had a bit of trouble initially echoing the obfuscated code on-screen rather than it converting back to plain text.

  2. Victor Kimura using Mozilla Compatible 5.0 on Windows 2000 | May 12, 2007 at 6:50 am | Permalink

    Nice code. I did receive the following error "reference not terminated by REFC delimiter." when I was trying to validate the XHTML at the W3C site. It validates as XHTML Strict but the error came up. So all I did was add the the semi-colon to each character so the last line now reads as: $obfuscatedEmail .= "&#" . ord($email[$i]). ";";

    Thanks for the code. Super simple.

  3. iMhatimi using Opera 9.62 on Windows XP | December 29, 2008 at 12:22 am | Permalink

    Yes bro all right ;)

  4. donarno using Firefox 3.0.6 on Windows XP | March 1, 2009 at 7:16 am | Permalink

    I've adapted your script as a function to read 120 email address and obfuscate and with an image as clicking place
    Wonderful !

    Thx

  5. Dunken using Firefox 3.5.7 on Windows 7 | January 12, 2010 at 10:26 am | Permalink

    If you have multiple email addresses on your web site you may want to use something like Privatedaddy.com which is an open source script that i have used in 2 projects (one commercial and one hobby project)
    But thank you in case i need a shorter/simpler solution i have kept your code in my snippet bin for sure :-)

  6. Peter using Google Chrome 7.0.517.44 on Windows 7 | November 11, 2010 at 11:38 am | Permalink

    Easy and simple. Thanks for sharing!

{ 2 } Trackbacks

  1. [...] I have to give where credit is due so thanks to Aaron Toponce for this Obfuscate email PHP style. [...]

  2. [...] the obfuscation in the theme’s PHP file. I’ve been using a solution originally devised by Aaron Toponce, but with a few [...]

Post a Comment

Your email is never published nor shared.

Switch to our mobile site