-
Notifications
You must be signed in to change notification settings - Fork 1
/
urls.php
executable file
·51 lines (41 loc) · 1.34 KB
/
urls.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
class urls {
private static $tlds = false;
static function parse($url){
$infos = is_array($url)?$url:parse_url($url);
$host = $infos['host'];
if($host) {
$infos = array_merge($infos, self::parse_host($host));
if(!$infos['path']) $infos['path'] = '/';
}
return $infos;
}
private static function tlds(){
if(!self::$tlds)
self::$tlds = tlds::get_list();
return self::$tlds;
}
//return compact('domain', 'tld', 'sub', 'host')
static function parse_host($host){
$tlds = self::tlds();
$parts = explode('.', mb_strtolower($host));
$stack = false; $tld_level = 1; //unknown tld are 1st level
foreach(array_reverse($parts) as $part) {
$stack = $stack?"$part.$stack":$part;
if(!isset($tlds[$stack])) break;
$tld_level = $tlds[$stack];
}
if(false
|| count(array_filter($parts)) != count($parts)
|| count($parts)<=$tld_level )
throw new Exception("Invalid tld");
$tld = join('.', array_slice($parts, -$tld_level));
$domain = join('.', array_slice($parts, (-$tld_level-1)));
$sub = join('.', array_slice($parts, 0, (-$tld_level-1)));
return compact('domain', 'tld', 'sub', 'host');
}
public static function merge($url1, $url2){
$url = new url($url1);
return $url->merge(new url($url2));
}
}