php 獲取頂級域名
阿新 • • 發佈:2018-05-19
xpl pla lod ati AR brush data func php
/** * 獲取頂級域名 * @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)){ //防止鏈接帶有點 (.) 導致出錯 $url = mb_substr($url,0,$mix_num); } $data = explode(‘.‘, $url); $co_ta = count($data); //判斷是否是雙後綴 $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; } } //截取域名後的目錄 $del = strpos($data[$co_ta-1], ‘/‘); if($del > 0 || !empty($del)){ $data[$co_ta-1] = mb_substr($data[$co_ta-1],0,$del); } //如果是返回FALSE ,如果不是返回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 獲取頂級域名