Skip to content

Instantly share code, notes, and snippets.

@niksumeiko
Last active September 24, 2015 03:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niksumeiko/657918 to your computer and use it in GitHub Desktop.
Save niksumeiko/657918 to your computer and use it in GitHub Desktop.
<?php
function tep_rewrite_email($content) {
$email_patt = '([A-Za-z0-9._%-]+)\@([A-Za-z0-9._%-]+)\.([A-Za-z0-9._%-]+)';
$mailto_pattern = '#\<a[^>]*?href=\"mailto:\s?' . $email_patt . '[^>]*?\>[^>]*?<\/a\>#';
$rewrite_result = '<span class="mailme">\\1 AT \\2 DOT \\3</span>';
$content = preg_replace($mailto_pattern, $rewrite_result, $content);
$content = preg_replace('#' . $email_patt . '#', $rewrite_result, $content);
return $content;
}
?>
@niksumeiko
Copy link
Author

PHP function that finds emails in the given output and converts them to {{holder}} AT {{domain}} DOT {{zone}} format. I have created this function to fight against spam bots. The idea was to convert output into this custom format on the Backend side printing this into HTML page and then find, parse printed HTML element with JavaScript to encode it back to human-readable email format.

Example:
echo tep_rewrite_email('email@example.com');
// Prints <span class="mailme">email AT example DOT com</span>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment