PHP domain name verification
Question: How to verify domain name in PHP without using regular expressions?
Verification requirements:
Possible answers:
Domain name validation without using regular expressions is not possible as multiple rules and restrictions need to be checked.
Regular expression pattern:
/^[a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i
Meaning:
Sample code:
<code class="php">function is_valid_domain_name($domain_name) { return (preg_match("/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i", $domain_name) //valid chars check && preg_match("/^.{1,253}$/", $domain_name) //overall length check && preg_match("/^[^\.]{1,63}(\.[^\.]{1,63})*$/", $domain_name) ); //length of each label }</code>
Test case:
域名 | 有效性 |
---|---|
a | Y |
0 | Y |
a.b | Y |
localhost | Y |
google.com | Y |
news.google.co.uk | Y |
xn--fsqu00a.xn--0zwm56d | Y |
goo gle.com | N |
google..com | N |
google.com | N |
google-.com | N |
.google.com | N |
Popular Tutorials
More>
Latest Downloads
More>
|