Current Location: Home> Latest Articles> How to Verify Domain Format Correctness Using Regular Expressions and the checkdnsrr Function?

How to Verify Domain Format Correctness Using Regular Expressions and the checkdnsrr Function?

gitbox 2025-08-25

How to Verify Domain Format Correctness Using Regular Expressions and the checkdnsrr Function?

In PHP, validating whether a domain name has the correct format is a common requirement. Generally, there are two ways to do this: one is by using regular expressions to check the format, and the other is by using PHP’s built-in checkdnsrr function to verify whether the domain exists. This article will explore both methods in detail and demonstrate how to combine them for effective domain validation.

1. Validating Domain Format with Regular Expressions

Regular expressions are a powerful tool that can quickly check whether a string matches a specified pattern. When validating domain formats, we need to consider the following key elements:

  • A domain consists of letters, digits, hyphens, and dots (.).

  • Each label (the parts separated by dots) must contain at least one letter or digit, and it cannot begin or end with a hyphen.

  • The total length must not exceed 253 characters.

Here’s an example of a regular expression for validating domain formats:

<span><span><span class="hljs-function"><span class="hljs-keyword">function</span></span></span><span> </span><span><span class="hljs-title">validate_domain_format</span></span><span>(</span><span><span class="hljs-params"><span class="hljs-variable">$domain</span></span></span><span>) {
    </span><span><span class="hljs-variable">$pattern</span></span><span> = </span><span><span class="hljs-string">&#039;/^(?!-)[A-Za-z0-9-]{1,63}(?&lt;!-)\.(?!-)[A-Za-z0-9-]{1,63}(?&lt;!-)$/&#039;</span></span><span>;

}

Explanation:

  • ^(?!-): Ensures the domain does not start with a hyphen (-).

  • [A-Za-z0-9-]{1,63}: Each label of the domain can include letters, digits, and hyphens, with a length of 1 to 63 characters.

  • (?: Ensures the domain does not end with a hyphen (-).

  • \.: Matches the dot (.) character.

  • This regular expression ensures the validity and format of each part of the domain.

2. Validating Domain DNS Records with checkdnsrr

Although regular expressions can validate the format of a domain, they cannot confirm whether the domain actually exists. To ensure validity, we can use PHP’s built-in checkdnsrr function, which checks whether a domain has corresponding DNS records. If valid DNS records exist, the domain is real and accessible.

<span><span><span class="hljs-function"><span class="hljs-keyword">function</span></span></span><span> </span><span><span class="hljs-title">check_domain_exists</span></span>(</span><span><span class="hljs-params"><span class="hljs-variable">$domain</span></span></span><span>) {
    </span><span><span class="hljs-keyword">return</span></span><span> </span><span><span class="hljs-title function_ invoke__">checkdnsrr</span></span>(</span><span><span class="hljs-variable">$domain</span></span>, </span><span><span class="hljs-string">"A"</span></span>); </span><span><span class="hljs-comment">// Checks for an A record, indicating domain validity</span></span>
}
</span></span>

Explanation:

  • checkdnsrr($domain, "A"): This function checks whether the domain has an A record, i.e., whether it is associated with an IP address. If so, it returns true, meaning the domain is valid.

  • You can also use the MX parameter to check for mail exchange records, or use the ANY parameter to check for any type of DNS record.

3. Combining Regular Expressions and checkdnsrr

To validate both the format and the existence of a domain, we can combine regular expressions with the checkdnsrr function. The following code demonstrates how to perform such combined validation:

<span><span><span class="hljs-function"><span class="hljs-keyword">function</span></span></span><span> </span><span><span class="hljs-title">validate_domain</span></span>(</span><span><span class="hljs-params"><span class="hljs-variable">$domain</span></span></span><span>) {
    </span><span><span class="hljs-comment">// First, validate the domain format</span></span>
    </span><span><span class="hljs-keyword">if</span></span> (!</span><span><span class="hljs-title function_ invoke__">validate_domain_format</span></span>(</span><span><span class="hljs-variable">$domain</span></span>)) {
        </span><span><span class="hljs-keyword">return</span></span> </span><span><span class="hljs-string">"Invalid domain format"</span></span>;
    }
</span><span><span class="hljs-keyword">if</span></span> (!</span><span><span class="hljs-title function_ invoke__">check_domain_exists</span></span>(</span><span><span class="hljs-variable">$domain</span></span>)) {
    </span><span><span class="hljs-keyword">return</span></span> </span><span><span class="hljs-string">"Domain does not exist"</span></span>;
}

</span><span><span class="hljs-keyword">return</span></span> </span><span><span class="hljs-string">"Domain format is valid and exists"</span></span>;

}

Usage Example:

<span><span><span class="hljs-variable">$domain</span></span> = </span><span><span class="hljs-string">"example.com"</span></span>;
<p></span>$result = </span>validate_domain(</span>$domain);<br>
</span>echo </span>$result; </span>// Outputs "Domain format is valid and exists"<br>
</span></span>

4. Other Considerations

  • Internationalized Domain Names (IDN): If your application needs to support internationalized domain names (domains containing non-ASCII characters), you can use the idn_to_ascii() function to convert them into ASCII before validation.

  • Domain Length Limit: According to standards, a domain name must not exceed 253 characters in total length. If a user inputs a domain longer than this, an additional check is necessary.

  • Error Handling: When calling the checkdnsrr function, DNS resolution errors may occur. It is recommended to use the @ symbol to suppress warnings and handle errors based on the actual situation.

Conclusion

By combining regular expressions with the checkdnsrr function, PHP developers can efficiently validate both the format and the existence of domains. Regular expressions ensure that the format follows standards, while checkdnsrr further confirms whether the domain actually exists. This dual-layer validation greatly improves accuracy and reliability, helping to avoid issues caused by incorrect or non-existent domains.