Skip to content

Instantly share code, notes, and snippets.

@mullens
Last active May 8, 2018 17:39
Show Gist options
  • Select an option

  • Save mullens/9750940 to your computer and use it in GitHub Desktop.

Select an option

Save mullens/9750940 to your computer and use it in GitHub Desktop.
Outputs a crawl-safe email with a working "mailto:" link using JavaScript.
<?php
function safe_email($email){
$bademail = str_replace("@"," [at] ",$email);
$emailid = rand(0,1000000000000);
$data = "<a href='#' id='email$emailid'>$bademail</a>";
$data .= '<script type="text/javascript">';
$jsemail = substr($email, 0, 4).'" + "'.substr($email, 4);
$data .= 'jQuery("#email'.$emailid.'").html("'.$jsemail.'").attr("href","mailto:'.$jsemail.'")';
$data .= '</script>';
return $data;
};
//USAGE
echo safe_email("[email protected]");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment