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= "";                // 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 | 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. iMhatimi | December 29, 2008 at 12:22 am | Permalink

    Yes bro all right 😉

  3. Victor Kimura | 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.

  4. donarno | 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 !


  5. Dunken | 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 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 | 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.