| Conditions | 6 |
| Paths | 8 |
| Total Lines | 32 |
| Code Lines | 17 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 13 | static public function getMainDomain($url) { |
||
| 14 | $url = strtolower($url);//首先转成小写 |
||
| 15 | $host = parse_url($url, PHP_URL_HOST); // 获取host部分 |
||
| 16 | if(empty($host)){ |
||
| 17 | $url = "https://".$url; |
||
| 18 | $host = parse_url($url,PHP_URL_HOST); |
||
| 19 | } |
||
| 20 | if (!$host) { |
||
| 21 | return ''; |
||
| 22 | } |
||
| 23 | if (in_array($host,['127.0.0.1','localhost'])) { |
||
| 24 | return ''; |
||
| 25 | } |
||
| 26 | |||
| 27 | // 假设主域名至少有两个部分(例如:example.com),并且不包含子域名 |
||
| 28 | $parts = explode('.', $host); |
||
| 29 | $count = count($parts); |
||
| 30 | |||
| 31 | /* |
||
| 32 | * 通常情况下,主域名由最后两个部分组成 |
||
| 33 | * 但这可能不适用于所有情况,特别是当TLD是.co.uk这样的 |
||
| 34 | * 判断是否是双后缀aaa.com.cn |
||
| 35 | */ |
||
| 36 | $preg = '/[\w].+\.(com|net|org|gov|edu)\.cn$/'; |
||
| 37 | if ($count > 2 && preg_match($preg,$host)){ |
||
| 38 | //双后缀取后3位 |
||
| 39 | $host = $parts[$count-3].'.'.$parts[$count-2].'.'.$parts[$count-1]; |
||
| 40 | } else{ |
||
| 41 | // 如果只有两个部分,则直接返回整个host作为主域名 |
||
| 42 | $host = $parts[$count-2].'.'.$parts[$count-1]; |
||
| 43 | } |
||
| 44 | return $host; |
||
| 45 | } |
||
| 58 |