Skip to content

Instantly share code, notes, and snippets.

@DasLampe
Created December 2, 2011 00:19
Show Gist options
  • Save DasLampe/1420919 to your computer and use it in GitHub Desktop.
Save DasLampe/1420919 to your computer and use it in GitHub Desktop.
Find emailaddresses, make clickable and encode with base64
/********Code based on https://gist.github.com/907604 *********
The code find email addresses and proteceds for bots.
Make the addresses clickable with webtoolkit.base64.js and the following code (JQuery requiered):
$('a').each(function() {
var href, mailaddress;
href = $(this).attr('href');
if(href.search(/mailto:/) != -1)
{
mailaddress = href.substr(6);
mailaddress = Base64.decode(mailaddress);
$(this).attr('href', 'mailto:'+mailaddress);
if($(this).html().search(/ät|at/) != -1)
{
$(this).html(mailaddress);
}
}
});
********************************************************************/
function make_email_clickable($html) {
$regexp = '/
( # leading text
<\w+.?>| # leading HTML tag, or
[^=:!\'"\/]| # leading punctuation, or
<a.?href="mailto:|
^ # beginning of line
)
(
[a-z]+[a-z0-9\-\.\_]+?@[a-z0-9\-\.]+[a-z]{2,6}
)
(
[[:punct:]]||\s|<|$
) # trailing text
/x';
return preg_replace_callback($regexp, function($matched) {
list($all, $before, $address, $after) = $matched;
// already linked
if (preg_match('/<a\s/i', $before)) {
return preg_replace_callback("/[a-z]+[a-z0-9\-\.\_]+?@[a-z0-9\-\.]+[a-z]{2,6}/x", function($matches){return base64_encode($matches[0]);}, $all);
}
$text = strtr($address,array("@"=> " ät "));
$address = base64_encode($address);
return $before.'<a href="mailto:'.$address.'">'.$text.'</a>'.$after;
}, $html);
}
echo make_email_clickable("daslampe@lano-crew.org");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment