PHP regular to determine whether the string is a domain name method sharing

Source: Internet
Author: User
This article introduces, in PHP programming, using regular expressions to determine whether a string is a domain name method, the need for friends to refer to it.

Usually match the format requirements of the domain name:

1, the name of the domain names are composed of English letters and numbers, each marking no more than 63 characters, and does not distinguish between uppercase and lowercase letters. Other punctuation marks cannot be used except hyphens (-) in the label. 2, all levels of domain names with a real point (.) connection, the length of the three-level domain name can not exceed 20 characters. 3. The complete domain name consisting of multiple labels is not more than 255 characters in total.

Match the regular name of the domain:

1, by the English numerals and "_" Composition [-a-z0-9]2, each level to "." Connection 3, level Three cannot have a domain length not exceeding 20 {1,20}

To see an example,

 
      

The following regular expressions, all from the network, for everyone to learn the reference.

Regular 1\b ([a-z0-9]+ (-[a-z0-9]+) *\.) +[a-z]{2,}\b Error Match: Length >60 regular 2^ ([^-][a-z0-9a-z-_]+\.) *) [^-][a-z0-9a-z-_]+ (\.[ a-za-z]{2,4}) {1,2}$ error match: Length >60 error match: www.te_st.com regular 3 ([\w\d\-_]+\.):? [ ^-_]) +\w{2,4} mismatch: test.com.cn error matching: www.te_st.com regular 4[a-za-z0-9][-a-za-z0-9]{0,62} (. [ A-ZA-Z0-9][-A-ZA-Z0-9]{0,62}) +.? No error regular 5 (? <=\.) ([A-za-z0-9] ([a-za-z0-9-]{0,61}[a-za-z0-9])?.) +[a-za-z]{2,6} cannot match two-level domain name regular 6 ([A-z0-9][a-z0-9\-]*?\. (?: Com|cn|net|org|gov|info|la|cc|co) (?: \. (?: CN|JP))?) $ some top-level domains do not match, use caution.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.