1. 首页
  2. WordPress 函数手册

sanitize_email()

sanitize_email( string $email )

删除电子邮件中不允许的所有字符。
Strips out all characters that are not allowable in an email.

目录锚点:#参数#返回#源码#笔记


参数(Parameters)

参数 类型 必填 说明
$email (string) 必需 要筛选的电子邮件地址。

返回(Return)

(string)筛选的电子邮件地址。


源码(Source)

/**
 * Strips out all characters that are not allowable in an email.
 *
 * @since 1.5.0
 *
 * @param string $email Email address to filter.
 * @return string Filtered email address.
 */
function sanitize_email( $email ) {
	// Test for the minimum length the email can be
	if ( strlen( $email ) < 3="" )="" {="" *="" *="" filter="" a="" sanitized="" email="" address.="" *="" *="" this="" filter="" is="" evaluated="" under="" several="" contexts,="" including="" 'email_too_short',="" *="" 'email_no_at',="" 'local_invalid_chars',="" 'domain_period_sequence',="" 'domain_period_limits',="" *="" 'domain_no_periods',="" 'domain_no_valid_subs',="" or="" no="" context.="" *="" *="" @since="" 2.8.0="" *="" *="" @param="" string="" $email="" the="" sanitized="" email="" address.="" *="" @param="" string="" $email="" the="" email="" address,="" as="" provided="" to="" sanitize_email().="" *="" @param="" string="" $message="" a="" message="" to="" pass="" to="" the="" user.="" */="" return="" apply_filters(="" 'sanitize_email',="" '',="" $email,="" 'email_too_short'="" );="" }="" test="" for="" an="" @="" character="" after="" the="" first="" position="" if="" (="" strpos(="" $email,="" '@',="" 1="" )="==" false="" )="" {="" *="" this="" filter="" is="" documented="" in="" wp-includes/formatting.php="" */="" return="" apply_filters(="" 'sanitize_email',="" '',="" $email,="" 'email_no_at'="" );="" }="" split="" out="" the="" local="" and="" domain="" parts="" list(="" $local,="" $domain="" )="explode(" '@',="" $email,="" 2="" );="" local="" part="" test="" for="" invalid="" characters="" $local="preg_replace(" '/&#91;^a-za-z0-9!#$%&'*+\/="?^_`{|}~.-&#93;/'," '',="" $local="" );="" if="" (="" ''="==" $local="" )="" {="" *="" this="" filter="" is="" documented="" in="" wp-includes/formatting.php="" */="" return="" apply_filters(="" 'sanitize_email',="" '',="" $email,="" 'local_invalid_chars'="" );="" }="" domain="" part="" test="" for="" sequences="" of="" periods="" $domain="preg_replace(" '/.{2,}/',="" '',="" $domain="" );="" if="" (="" ''="==" $domain="" )="" {="" *="" this="" filter="" is="" documented="" in="" wp-includes/formatting.php="" */="" return="" apply_filters(="" 'sanitize_email',="" '',="" $email,="" 'domain_period_sequence'="" );="" }="" test="" for="" leading="" and="" trailing="" periods="" and="" whitespace="" $domain="trim(" $domain,="" "="" x0b."="" );="" if="" (="" ''="==" $domain="" )="" {="" *="" this="" filter="" is="" documented="" in="" wp-includes/formatting.php="" */="" return="" apply_filters(="" 'sanitize_email',="" '',="" $email,="" 'domain_period_limits'="" );="" }="" split="" the="" domain="" into="" subs="" $subs="explode(" '.',="" $domain="" );="" assume="" the="" domain="" will="" have="" at="" least="" two="" subs="" if="" (="" 2=""> count( $subs ) ) {
		/** This filter is documented in wp-includes/formatting.php */
		return apply_filters( 'sanitize_email', '', $email, 'domain_no_periods' );
	}

	// Create an array that will contain valid subs
	$new_subs = array();

	// Loop through each sub
	foreach ( $subs as $sub ) {
		// Test for leading and trailing hyphens
		$sub = trim( $sub, " 	

x0B-" );

		// Test for invalid characters
		$sub = preg_replace( '/[^a-z0-9-]+/i', '', $sub );

		// If there's anything left, add it to the valid subs
		if ( '' !== $sub ) {
			$new_subs[] = $sub;
		}
	}

	// If there aren't 2 or more valid subs
	if ( 2 > count( $new_subs ) ) {
		/** This filter is documented in wp-includes/formatting.php */
		return apply_filters( 'sanitize_email', '', $email, 'domain_no_valid_subs' );
	}

	// Join valid subs into the new domain
	$domain = join( '.', $new_subs );

	// Put the email back together
	$email = $local . '@' . $domain;

	// Congratulations your email made it!
	/** This filter is documented in wp-includes/formatting.php */
	return apply_filters( 'sanitize_email', $email, $email, null );
}
更新版本 源码位置 使用 被使用
1.5.0 wp-includes/formatting.php:3603 4 2

笔记(Notes)

基本示例

sanitize_email() 为WP2原创文章,链接:https://www.wp2.cn/functions/sanitize_email/

发表评论

邮箱地址不会被公开。