Encode Email Address


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

With this snippet, you can encode any email address into HTML entities so that spam bots do not find it.


Copy this code and paste it in your HTML
  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. $encoded = '<script type="text/javascript">';
  18. $encoded .= "document.write('$part1');";
  19. $encoded .= "document.write('$part2');";
  20. foreach($email as $e)
  21. {
  22. $encoded .= "document.write('$e');";
  23. }
  24. $encoded .= "document.write('$part3');";
  25. foreach($linkText as $l)
  26. {
  27. $encoded .= "document.write('$l');";
  28. }
  29. $encoded .= "document.write('$part4');";
  30. $encoded .= '</script>';
  31.  
  32. return $encoded;
  33. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.