method isEmail documentation in bbn\Str
Checks if the string is the correct type of e-mail address.
Example
X::dump(\bbn\Str::isEmail('test@email.com'));
// (bool) true
X::dump(\bbn\Str::isEmail('test@email'));
// (bool) false
X::dump(\bbn\Str::isEmail('test@.com'));
// (bool) false
X::dump(\bbn\Str::isEmail('testemail.com'));
// (bool) false
function($email)
{
if (function_exists('filter_var')) {
return filter_var($email,FILTER_VALIDATE_EMAIL) ? true : false;
}
else
{
$isValid = true;
$atIndex = mb_strrpos($email, "@");
if (\is_bool($atIndex) && !$atIndex) {
$isValid = false;
}
else
{
$domain = mb_substr($email, $atIndex + 1);
$local = mb_substr($email, 0, $atIndex);
$localLen = mb_strlen($local);
$domainLen = mb_strlen($domain);
// local part length exceeded
if ($localLen < 1 || $localLen > 64) {
$isValid = false;
}
// domain part length exceeded
elseif ($domainLen < 1 || $domainLen > 255) {
$isValid = false;
}
// local part starts or ends with '.'
elseif ($local[0] == '.' || $local[$localLen - 1] == '.') {
$isValid = false;
}
// local part has two consecutive dots
elseif (mb_ereg_match('\\.\\.', $local)) {
$isValid = false;
}
// character not valid in domain part
elseif (!mb_ereg_match('^[A-Za-z0-9\\-\\.]+$', $domain)) {
$isValid = false;
}
// domain part has two consecutive dots
elseif (mb_ereg_match('\\.\\.', $domain)) {
$isValid = false;
}
// character not valid in local part unless
elseif (!mb_ereg_match(
'^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$'
,str_replace("\\\\","",$local)
)
) {
// local part is quoted
if (!mb_ereg_match('^"(\\\\"|[^"])+"$',str_replace("\\\\","",$local))) {
$isValid = false;
}
}
}
return $isValid;
}
}
Checks if the string is the correct type of e-mail address. BBN is a suite of PHP and JS libraries and VueJS components - all open-source! bbn.io, build applications, the quick way
This website uses cookies to ensure you get the best experience on our website.
© 2011-2025
BBN Solutions