antispambot()将 Email 地址的字符转化成 HTML entities 以便防止垃圾邮件

目录

描述

译文

将电子邮件地址的字符转换为HTML实体以阻止广告机器人。

原文

将 Email 地址的字符转化成 HTML entities 以便防止垃圾邮件。

antispambot() 描述

用法

<?php antispambot$emailaddy$hex_encoding ?>

antispambot() 用法

参数

$emailaddy

(string) (必填) Email 地 

默认值: None

$hex_encoding

(integer) (可选) 0 或者 1. 用 编码.

 值: 0

antispambot() 参数

返回值

(string) 

转化之后的 Email 地址。

antispambot() 返回值

示例

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

/**
 * 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 wpcodex_hide_email_shortcode( $atts , $content = null ) {
	if ( ! is_email( $content ) ) {
		return;
	}

	return '<a href="mailto:' . antispambot( $content ) . '">' . antispambot( $content ) . '</a>';
}
add_shortcode( 'email', 'wpcodex_hide_email_shortcode' );

To use this in your WordPress Content area all you have to do it wrap it in a short code.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

[email]john.doe@mysite.com[/email]

You can also use this in a plain text widget if you add this filter to your function file as well.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

add_filter( 'widget_text', 'shortcode_unautop' );
add_filter( 'widget_text', 'do_shortcode' );

Default Usage

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?php
echo antispambot( 'john.doe@mysite.com' );
?>

This will output the email like this in the HTML:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

&#106;&#111;h&#110;&#46;&#100;&#111;&#101;&#64;mysit&#101;.&#99;&#111;&#109;

But it will appear as a normal email address to anyone using a web browser:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

john.doe@mysite.com

antispambot() 示例

历史

添加于 版本: 0.71

antispambot() 历史

源文件

antispambot() 函数的代码位于 wp-includes/formatting.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * 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, $len = strlen( $email_address ); $i < $len;="" $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="" );="" }="" }="" return="" str_replace(="" '@',="" '@',="" $email_no_spam_address="" );="" }="">

antispambot() 源文件