is_email()
云策文档标注
概述
is_email() 函数用于验证电子邮件地址的有效性,但存在一些限制,如不支持国际化域名且不完全符合 RFC 标准。它返回验证后的电子邮件地址或 false。
关键要点
- 函数验证电子邮件地址,但可能无法正确处理国际化域名或某些无效字符。
- 参数包括 $email(必需,要验证的电子邮件地址)和 $deprecated(可选,已弃用,默认 false)。
- 返回值:成功时返回有效的电子邮件地址字符串,失败时返回 false。
- 使用 apply_filters('is_email', ...) 钩子来过滤验证结果。
- 函数内部通过正则表达式和字符串处理进行验证,例如检查长度、格式和字符有效性。
代码示例
if ( is_email( 'email@domain.com' ) ) {
echo 'email address is valid.';
}注意事项
- 此函数不兼容国际化域名,且不完全符合 RFC 标准,可能在某些边缘情况下返回错误结果。
- 已弃用参数 $deprecated 在 WordPress 3.0.0 后不再推荐使用。
原文内容
Verifies that an email is valid.
Description
Does not grok i18n domains. Not RFC compliant.
Parameters
$emailstringrequired-
Email address to verify.
$deprecatedbooloptional-
Deprecated.
Default:
false
Source
function is_email( $email, $deprecated = false ) {
if ( ! empty( $deprecated ) ) {
_deprecated_argument( __FUNCTION__, '3.0.0' );
}
// Test for the minimum length the email can be.
if ( strlen( $email ) < 6 ) {
/**
* Filters whether an email address is valid.
*
* This filter is evaluated under several different contexts, such as 'email_too_short',
* 'email_no_at', 'local_invalid_chars', 'domain_period_sequence', 'domain_period_limits',
* 'domain_no_periods', 'sub_hyphen_limits', 'sub_invalid_chars', or no specific context.
*
* @since 2.8.0
*
* @param string|false $is_email The email address if successfully passed the is_email() checks, false otherwise.
* @param string $email The email address being checked.
* @param string $context Context under which the email was tested.
*/
return apply_filters( 'is_email', false, $email, 'email_too_short' );
}
// Test for an @ character after the first position.
if ( false === strpos( $email, '@', 1 ) ) {
/** This filter is documented in wp-includes/formatting.php */
return apply_filters( 'is_email', false, $email, 'email_no_at' );
}
// Split out the local and domain parts.
list( $local, $domain ) = explode( '@', $email, 2 );
/*
* LOCAL PART
* Test for invalid characters.
*/
if ( ! preg_match( '/^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~.-]+$/', $local ) ) {
/** This filter is documented in wp-includes/formatting.php */
return apply_filters( 'is_email', false, $email, 'local_invalid_chars' );
}
/*
* DOMAIN PART
* Test for sequences of periods.
*/
if ( preg_match( '/.{2,}/', $domain ) ) {
/** This filter is documented in wp-includes/formatting.php */
return apply_filters( 'is_email', false, $email, 'domain_period_sequence' );
}
// Test for leading and trailing periods and whitespace.
if ( trim( $domain, " tnrx0B." ) !== $domain ) {
/** This filter is documented in wp-includes/formatting.php */
return apply_filters( 'is_email', false, $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( 'is_email', false, $email, 'domain_no_periods' );
}
// Loop through each sub.
foreach ( $subs as $sub ) {
// Test for leading and trailing hyphens and whitespace.
if ( trim( $sub, " tnrx0B-" ) !== $sub ) {
/** This filter is documented in wp-includes/formatting.php */
return apply_filters( 'is_email', false, $email, 'sub_hyphen_limits' );
}
// Test for invalid characters.
if ( ! preg_match( '/^[a-z0-9-]+$/i', $sub ) ) {
/** This filter is documented in wp-includes/formatting.php */
return apply_filters( 'is_email', false, $email, 'sub_invalid_chars' );
}
}
// Congratulations, your email made it!
/** This filter is documented in wp-includes/formatting.php */
return apply_filters( 'is_email', $email, $email, null );
}
Hooks
- apply_filters( ‘is_email’, string|false $is_email, string $email, string $context )
-
Filters whether an email address is valid.
Changelog
| Version | Description |
|---|---|
| 0.71 | Introduced. |
Skip to note 2 content
Codex
Example
if ( is_email( 'email@domain.com' ) ) { echo 'email address is valid.'; }