antispambot()

最后更新于:2021-11-25 19:47:48

antispambot( string$email_address, int$hex_encoding)

Converts email addresses characters to HTML entities to block spam bots.

参数

$email_address

(string) (Required) Email address.

$hex_encoding

(int) (Optional) Set to 1 to enable hex encoding.

响应

(string) Converted email address.

源文件

文件: gc-includes/formatting.php

function antispambot( $email_address, $hex_encoding = 0 ) {
	$email_no_spam_address = '';
	for ( $i = 0, $len = strlen( $email_address ); $i < $len; $i++ ) {
		$j = rand( 0, 1 + $hex_encoding );
		if ( 0 == $j ) {
			$email_no_spam_address .= '&#' . ord( $email_address[ $i ] ) . ';';
		} elseif ( 1 == $j ) {
			$email_no_spam_address .= $email_address[ $i ];
		} elseif ( 2 == $j ) {
			$email_no_spam_address .= '%' . zeroise( dechex( ord( $email_address[ $i ] ) ), 2 );
		}
	}

	return str_replace( '@', '&#64;', $email_no_spam_address );
}
/**
 * Hide email from Spam Bots using a shortcode.
 *
 * @param array  $atts    Shortcode attributes. Not used.
 * @param string $content The shortcode content. Should be an email address.
 * @return string The obfuscated email address. 
 */
function gcdocs_hide_email_shortcode( $atts , $content = null ) {
	if ( ! is_email( $content ) ) {
		return;
	}
	return '<a href="' . esc_url('mailto:' . antispambot( $content ) ) . '">' . esc_html( antispambot( $content ) ) . '</a>';
}
add_shortcode( 'email', 'gcdocs_hide_email_shortcode' );