Core\Validate::isEmail
Core\Validate::isEmail(string $s): boolCore\Validate::isEmail( string $s, ): bool
Checks whether a text is an email address, such as ada@example.com. The method returns true or false.
An address has one @. The part before it has at most 64 characters: English letters, digits, single dots and some symbols such as +, - and _. It does not start or end with a dot. The part after the @ is a domain name with at least one dot, as Core\Validate::isDomain checks it. The whole address has at most 254 characters.
A space, two dots in a row, a letter with an accent or a second @ give false. So does an address without a dot after the @, such as admin@localhost. Core\Validate::isEmail checks only how the address is written. It does not check whether the address exists or can receive email.
The examples below check a few addresses, show the length limit of the part before the @, and check a list of addresses pasted into a form.
Parameters
$sstringThe text to test.
Return value
Returns bool. true for an email address. false for anything else, for example a space, two dots in a row, quotes, an address such as a@[192.0.2.1], a domain without a dot such as a@localhost, or a character that is not ASCII. The method does not check whether the address exists.
Examples
Check a few addresses
<?nvs// Shows which texts are email addresses.
// The first two are addresses. "admin@localhost" has no dot after the `@`.// The others contain two dots in a row, a space, or no `@` at all.array<string> $texts = ['ada@example.com', 'ada.lovelace+news@mail.example.org', 'admin@localhost', 'ada..l@example.com', 'ada @example.com', 'example.com'];
// `Core\Validate::isEmail` returns `true` for an email address.// This prints yes for the first two texts and no for the other four.foreach ($texts as string $text) { echo $text, ': ', Core\Validate::isEmail($text) ? 'yes' : 'no', "\n";}ada@example.com: yesada.lovelace+news@mail.example.org: yesadmin@localhost: noada..l@example.com: noada @example.com: noexample.com: noThe part before the at sign
<?nvs// Shows the length limit of the part before the `@`.
// The part before the `@` may have up to 64 characters.string $name = Core\Str::repeat('a', 64);string $longer = $name . 'a';
// This prints 64 yes, then 65 no.echo Core\Str::length($name), ' ', Core\Validate::isEmail($name . '@example.com') ? 'yes' : 'no', "\n";echo Core\Str::length($longer), ' ', Core\Validate::isEmail($longer . '@example.com') ? 'yes' : 'no', "\n";64 yes65 noCheck a pasted list
<?nvs// Shows how to check a list of addresses that a user pasted into a form.
// The user separated the addresses with commas and added some spaces.string $pasted = 'ada@example.com, grace@example.org,bob@@example.com , linus@localhost';
array<string> $valid = [];array<string> $invalid = [];
// `split` makes one text for each address, and `trim` removes the spaces.foreach (Core\Str::split($pasted, ',') as string $part) { string $address = Core\Str::trim($part); if (Core\Validate::isEmail($address)) { $valid[] = $address; } else { $invalid[] = $address; }}
// This prints the two valid addresses on the first line and the two others on the second.echo 'send to: ', Core\Str::join($valid, ', '), "\n";echo 'please check: ', Core\Str::join($invalid, ', '), "\n";send to: ada@example.com, grace@example.orgplease check: bob@@example.com, linus@localhost