Skip to content
PHP Logo

How can PHP scripts efficiently handle different types of domain extensions when checking availability?

When checking domain availability, PHP scripts can efficiently handle different types of domain extensions by using a combination of regular expressions and domain extension lists. By extracting the domain extension from the input domain name and comparing it against a list of valid extensions, the script can determine if the domain is valid. This approach allows for flexibility in handling various domain extensions without the need for hardcoding each one individually.

php
// List of valid domain extensions
$validExtensions = ['com', 'net', 'org', 'io', 'co', 'info'];

// Extract the domain extension from the input domain name
$domain = 'example.com';
$domainParts = explode('.', $domain);
$extension = end($domainParts);

// Check if the domain extension is valid
if (in_array($extension, $validExtensions)) {
    echo "Domain extension is valid.";
} else {
    echo "Invalid domain extension.";
}