This article introduces how to use regular expressions to determine whether a string is a domain name in PHP programming. Friends in need can refer to it.
Usually matching the format requirements of domain names: 1. The labels in the domain name are composed of English letters and numbers. Each label does not exceed 63 characters, and does not distinguish between uppercase and lowercase letters. No other punctuation marks can be used in labels except hyphens (-). 2. Domain names at all levels are connected with dots (.). The length of third-level domain names cannot exceed 20 characters. 3. A complete domain name composed of multiple labels must not exceed 255 characters in total.Regular rules for matching domain names: 1. Composed of English numbers and “_” [-a-z0-9] 2. Each level must be connected with "." 3. The length of the third-level domain name cannot exceed 20 {1,20}Let’s see an example, <?php $url = 'www.test.com'; $search = '/---正则N---/'; if(preg_match($search,$url)){ echo '匹配'; }else { echo '不匹配'; } //edit by bbs.it-home.org ?> Copy after login The following regular expressions are all sourced from the Internet for your reference. Regular 1 b([a-z0-9]+(-[a-z0-9]+)*.)+[a-z]{2,}b Bad match: length>60 Rule 2 ^(([^-][a-z0-9A-Z-_]+.)*)[^-][a-z0-9A-Z-_]+(.[a-zA-Z]{2 ,4}){1,2}$ Bad match: length>60 Bad match: www.te_st.com Rule 3 (([wd-_]+.):?[^-_])+w{2,4} Not matching: test.com.cn Bad match: www.te_st.com Rule 4 [a-zA-Z0-9][-a-zA-Z0-9]{0,62}(.[a-zA-Z0-9][-a-zA-Z0-9]{0,62} )+.? No errors yet Rule 5 (? |