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.

{ 5 } Comments
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.
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.
Yes bro all right
I’ve adapted your script as a function to read 120 email address and obfuscate and with an image as clicking place
Wonderful !
Thx
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
{ 2 } Trackbacks
[...] I have to give where credit is due so thanks to Aaron Toponce for this Obfuscate email PHP style. [...]
[...] 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