How to validate a domain name using Regex & PHP?
The accepted answer is incomplete/wrong. The regex pattern; should NOT validate domains such as: -example.com, example–.com, -example-.-.com, example.000, etc… should validate domains such as: schools.k12, newTLD.clothing, good.photography, etc… After some further research; below is the most correct, cross-language and compact pattern I could come up with: ^(?!\-)(?:(?:[a-zA-Z\d][a-zA-Z\d\-]{0,61})?[a-zA-Z\d]\.){1,126}(?!\d+)[a-zA-Z\d]{1,63}$ This pattern conforms with most* of the rules … Read more