Skip to content

Instantly share code, notes, and snippets.

@forsvunnet
Created September 9, 2013 12:44
Show Gist options
  • Save forsvunnet/6495090 to your computer and use it in GitHub Desktop.
Save forsvunnet/6495090 to your computer and use it in GitHub Desktop.
A filter to automatically link email addresses in a spam-safe manner
<?php
function driv_auto_email($text) {
$matches = array();
if (preg_match('/[A-Za-z\.\-\_]*@[A-Za-z\-\_]*\.[A-Za-z\.]*/', $text, $matches)) {
foreach ($matches as $match) {
$email = '';
$chars = array();
for ($i=0, $len = strlen($match); $i < $len; $i++) {
$chars[] = ord($match[$i]);
}
$script = "var t = [" . implode(', ', $chars) . "]; var email = '&#' + t.join(';&#') + ';'; document.write('<a href=\"mailto:' + email + '\">' + email + '</a>');";
$tag = '<script type="text/javascript">%s</script>';
$text = str_replace($match, sprintf($tag, $script), $text);
}
}
return $text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment