Skip to content

Core\Validate::isDomain

Checks whether a text is a domain name, such as example.com or mail.example.org. The method returns true or false.

A domain name has one or more parts joined by dots. Each part has 1 to 63 characters: English letters, digits and -. A part does not start or end with -. The whole name has at most 253 characters.

localhost has only one part, and it is a domain name. A name of digits such as 1.2.3.4 is one too. A dot at the end, two dots in a row, a space or a letter with an accent such as ü give false. Core\Validate::isDomain checks only how the name is written. It does not check whether the name exists on the internet.

The examples below check a few names, show the two length limits, and check the domain name a customer types into a settings form.

Parameters

  • $sstringThe text to test.

Return value

Returns bool. true for a domain name. localhost and a name of digits such as 1.2.3.4 are true too. false for an empty part, a dot at the end, a part that starts or ends with -, or a character that is not ASCII. The method does not check whether the name exists.

Examples

Check a few names

<?nvs
// Shows which texts are domain names.
// "-shop.test" starts a part with "-", and "a..b" has an empty part.
// "example.com." ends with a dot, and "münchen.de" contains "ü".
array<string> $names = ['example.com', 'mail.example.org', 'localhost', '-shop.test', 'a..b', 'example.com.', 'münchen.de'];
// `Core\Validate::isDomain` returns `true` for a domain name.
// This prints yes for the first three names and no for the other four.
foreach ($names as string $name) {
echo $name, ': ', Core\Validate::isDomain($name) ? 'yes' : 'no', "\n";
}
Output
example.com: yes
mail.example.org: yes
localhost: yes
-shop.test: no
a..b: no
example.com.: no
münchen.de: no

The two length limits

<?nvs
// Shows the two length limits of a domain name.
// One part may have up to 63 characters. This prints yes, then no.
string $part = Core\Str::repeat('a', 63);
echo Core\Validate::isDomain($part . '.test') ? 'yes' : 'no', "\n";
echo Core\Validate::isDomain($part . 'a.test') ? 'yes' : 'no', "\n";
// The whole name may have up to 253 characters.
// Three parts of 63 characters, one part of 61 and three dots make 253.
string $name = $part . '.' . $part . '.' . $part . '.' . Core\Str::repeat('b', 61);
string $longer = $name . 'b';
// This prints 253 yes, then 254 no.
echo Core\Str::length($name), ' ', Core\Validate::isDomain($name) ? 'yes' : 'no', "\n";
echo Core\Str::length($longer), ' ', Core\Validate::isDomain($longer) ? 'yes' : 'no', "\n";
Output
yes
no
253 yes
254 no

Check a shop domain

<?nvs
// Shows how to check the domain name a customer types into a settings form.
class ShopDomain
{
// This function returns an error message, or an empty text when the name is fine.
public static function problem(string $name): string
{
if (Core\Str::startsWith($name, 'http://') || Core\Str::startsWith($name, 'https://')) {
return 'enter only the name, without http:// or https://';
}
if (!Core\Validate::isDomain($name)) {
return 'this is not a domain name';
}
// `isDomain` accepts a name with one part, such as "localhost".
// A shop on the internet needs at least two parts.
if (!Core\Str::contains($name, '.')) {
return 'enter the full name, such as shop.example.com';
}
return '';
}
}
// Four names as customers typed them.
// `trim` removes the spaces and `lower` changes "Shop" to "shop".
array<string> $inputs = [' Shop.Example.com ', 'https://shop.example.com', 'my shop.com', 'myshop'];
// Only the first name is saved. The other three names each print their problem.
foreach ($inputs as string $input) {
string $name = Core\Str::lower(Core\Str::trim($input));
string $problem = ShopDomain::problem($name);
echo $name, ': ', $problem == '' ? 'saved' : $problem, "\n";
}
Output
shop.example.com: saved
https://shop.example.com: enter only the name, without http:// or https://
my shop.com: this is not a domain name
myshop: enter the full name, such as shop.example.com