Skip to content

Instantly share code, notes, and snippets.

@milesleif
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save milesleif/7cecf92c749a3d754918 to your computer and use it in GitHub Desktop.

Select an option

Save milesleif/7cecf92c749a3d754918 to your computer and use it in GitHub Desktop.
Encode eMails-adddresses to unicode
// used by encode_emails
// available in wp-core
function antispambot( $email_address, $hex_encoding = 0 ) {
$email_no_spam_address = '';
for ( $i = 0; $i < strlen( $email_address ); $i++ ) {
$j = rand( 0, 1 + $hex_encoding );
if ( $j == 0 ) {
$email_no_spam_address .= '&#' . ord( $email_address[$i] ) . ';';
} elseif ( $j == 1 ) {
$email_no_spam_address .= $email_address[$i];
} elseif ( $j == 2 ) {
$email_no_spam_address .= '%' . zeroise( dechex( ord( $email_address[$i] ) ), 2 );
}
}
$email_no_spam_address = str_replace( '@', '&#64;', $email_no_spam_address );
return $email_no_spam_address;
}
function encode_emails($string){
$pattern = "/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,3})(?:\.[a-z]{2})?/i";
preg_match_all($pattern, $string, $split);
foreach ($split[0] as $value) {
$clean_value = antispambot($value);
$string = str_replace($value, $clean_value ,$string);
}
return $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment