Mask Email With ASCII Character Codes In PHP

28th April 2008 - 2 minutes read time

Hiding your email address in an image is the best way of encrypting your email, but if your server doesn't support the GD2 library, or if you don't want to use it, then you might want to look at a different way of doing this.

The easiest way to encrypt your email address is to turn every character into the ASCII code equivalent and use this to display the text in HTML by putting a in front of each character. Here is a function that takes a string and turns it into HTML encoded text.

  1. function maskEmail($email) {
  2. $hiddenEmail = '';
  3. $length = strlen($email);
  4.  
  5. for ($i = 0;$i < $length; $i++) {
  6. $hiddenEmail .= "&#".ord($email[$i]).";";
  7. }
  8.  
  9. return $hiddenEmail;
  10. }

To use this just feed an email address into it.

  1. $hiddenEmail = maskEmail("[email protected]");
  2. echo $hiddenEmail;

This produces the following output in the source code (the browser will display this string as the email address you put in.

&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;

This can be easily added to an anchor tag to make an email link.

echo '<a href="mailto:'.$hiddenEmail.'" title="Email me">'.$hiddenEmail.'</a>';

 

Add new comment

The content of this field is kept private and will not be shown publicly.