E-mail encoder to reduce spam


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /**
  2.  * Description: E-Mail encoder to reduce spam.
  3.  *
  4.  * @author Micke Johansson
  5.  *
  6.  * $mail. The e-mail address to encode.
  7.  * $isLink. Set to true to create a link.
  8.  * $display. What will be displayed in the browser. If omitted it will display the e-mail address.
  9.  *
  10.  * @param string $mail
  11.  * @param bool $isLink
  12.  * @param string $display
  13.  * @return string Encoded e-mail or e-mail link
  14.  */
  15. function EncodeMail($mail, $isLink = false, $display = '')
  16. {
  17. $domain = substr($mail,strpos($mail, '@')+1);
  18. $name = substr($mail,0, strpos($mail, '@'));
  19. $encodedDomain = '';
  20. $encodedName = '';
  21. $encodedDisplay = '';
  22.  
  23. for ($i=0; $i < strlen($domain); $i++)
  24. {
  25. $encodedDomain .= '&#'.ord(substr($domain,$i)).';';
  26. }
  27. for ($i=0; $i < strlen($name); $i++)
  28. {
  29. $encodedName .= '&#'.ord(substr($name,$i)).';';
  30. }
  31. for ($i=0; $i < strlen($display); $i++)
  32. {
  33. $encodedDisplay .= '&#'.ord(substr($display,$i)).';';
  34. }
  35. $script = "<script type=\"text/javascript\">";
  36. $script .= "d=\"".$encodedDomain."\";";
  37. $script .= "n=\"".$encodedName."\";";
  38. if ($isLink)
  39. {
  40. if ($display == '')
  41. $script .= "document.write('<a href=\"&#109;&#097;&#105;&#108;&#116;&#111;&#058;'+n+'&#64;'+d+'\">'+n+'&#64;'+d+'</a>');";
  42. else
  43. $script .= "document.write('<a href=\"&#109;&#097;&#105;&#108;&#116;&#111;&#058;'+n+'&#64;'+d+'\">".$encodedDisplay."</a>');";
  44. }
  45. else
  46. {
  47. $script .= "document.write(n+'&#64;'+d);";
  48. }
  49. $script .= "</script>";
  50. return $script;
  51. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.