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= "email@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.