Mask Email With ASCII Character Codes In PHP

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.

function maskEmail($email) {
 $hiddenEmail = '';
 $length = strlen($email);    
 
 for ($i = 0;$i < $length; $i++) {
  $hiddenEmail .= "&#".ord($email[$i]).";";
 }
 
 return $hiddenEmail;
}

To use this just feed an email address into it.

$hiddenEmail = maskEmail("[email protected]");
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.
CAPTCHA
10 + 3 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.