Created
September 9, 2013 12:44
-
-
Save forsvunnet/6495090 to your computer and use it in GitHub Desktop.
A filter to automatically link email addresses in a spam-safe manner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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