Tag:substr Prevent min rpo sub div empty ++ domain
/** * Gets the top-level domain name * @param $url * @return String */public static function GetDomain ($url) {if (Empty ( $url)) {return '; if (Strpos ($url, ' http://')!== false) {$url = Str_replace (' http://', ', $url); } if (Strpos ($url, ' https://')!== false) {$url = Str_replace (' https://', ' ', $url); } $n = 0; for ($i = 1; $i <= 3; $i + +) {$n = Strpos ($url, '/', $n); $i! = 3 && $n + +; } $nn = Strpos ($url, '? '); $mix _num = min ($n, $nn); if ($mix _num > 0 | |!empty ($mix _num)) {//Prevent links with dots (.) causing an error $url = mb_substr ($url, 0, $mix _num); } $data = Explode ('. ', $url); $co _ta = count ($data); Determines whether a double suffix $no _tow = true; $host _cn = ' com.cn,net.cn,org.cn,gov.cn '; $host _CN = Explode (', ', $host _cn); foreach ($host _cn as $val) {if (Strpos ($url, $val)) {$no _tow = false; }}//The directory after intercepting the domain name $del = strpos ($data [$co _ta-1], '/'); if ($del > 0 | |!empty ($del)) {$data [$co _ta-1] = mb_substr ($data [$co _ta-1],0, $del); }//If it returns false if it is not true if ($no _tow = = True) {$host = $data [$co _ta-2]. $data [$co _ta-1]; }else{$host = $data [$co _ta-3]. $data [$co _ta-2]. $data [$co _ta-1]; } return $host; }
PHP get top-level domain