Skip to content

Instantly share code, notes, and snippets.

@gabrieleromanato
Created March 16, 2014 12:16
Show Gist options
  • Save gabrieleromanato/9582363 to your computer and use it in GitHub Desktop.
Save gabrieleromanato/9582363 to your computer and use it in GitHub Desktop.
WordPress: antispambot()
/**
* Converts email addresses characters to HTML entities to block spam bots.
*
* @since 0.71
*
* @param string $email_address Email address.
* @param int $hex_encoding Optional. Set to 1 to enable hex encoding.
* @return string Converted email address.
*/
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment