Email Encoder


 / Published in: PHP
 

This is a little function that encodes an email and hides it from spammers.

It creates a few lines of javascript code that displays the email on a website with a mailto link, custom text link and you can even add a class or something as an attribute to the tag.

  1. function encode_email($email='[email protected]', $linkText='Contact Us', $attrs ='class="emailencoder"' )
  2. {
  3. // remplazar aroba y puntos
  4. $email = str_replace('@', '@', $email);
  5. $email = str_replace('.', '.', $email);
  6. $email = str_split($email, 5);
  7.  
  8. $linkText = str_replace('@', '@', $linkText);
  9. $linkText = str_replace('.', '.', $linkText);
  10. $linkText = str_split($linkText, 5);
  11.  
  12. $part1 = '<a href="ma';
  13. $part2 = 'ilto&#58;';
  14. $part3 = '" '. $attrs .' >';
  15. $part4 = '</a>';
  16.  
  17. // generamos el Javascript
  18. $encoded = '<script type="text/javascript">';
  19. $encoded .= "document.write('$part1');";
  20. $encoded .= "document.write('$part2');";
  21. foreach($email as $e)
  22. {
  23. $encoded .= "document.write('$e');";
  24. }
  25. $encoded .= "document.write('$part3');";
  26. foreach($linkText as $l)
  27. {
  28. $encoded .= "document.write('$l');";
  29. }
  30. $encoded .= "document.write('$part4');";
  31. $encoded .= '</script>';
  32.  
  33. return $encoded;
  34. }

Report this snippet  

You need to login to post a comment.