Conditions | 4 |
Paths | 4 |
Total Lines | 22 |
Code Lines | 11 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
13 | static function getMainDomain($url) { |
||
|
|||
14 | $host = parse_url($url, PHP_URL_HOST); // 获取host部分 |
||
15 | if (!$host) { |
||
16 | return ''; |
||
17 | } |
||
18 | |||
19 | // 假设主域名至少有两个部分(例如:example.com),并且不包含子域名 |
||
20 | $parts = explode('.', $host); |
||
21 | $count = count($parts); |
||
22 | |||
23 | /* |
||
24 | * 通常情况下,主域名由最后两个部分组成 |
||
25 | * 但这可能不适用于所有情况,特别是当TLD是.co.uk这样的 |
||
26 | */ |
||
27 | if ($count > 2) { |
||
28 | return implode('.', array_slice($parts, -2)); // 返回最后两个部分的组合 |
||
29 | } elseif ($count == 2) { |
||
30 | // 如果只有两个部分,则直接返回整个host作为主域名 |
||
31 | return $host; |
||
32 | } else { |
||
33 | // 如果少于两个部分,可能不是一个有效的域名 |
||
34 | return ''; |
||
35 | } |
||
38 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.