标签:substr 防止 min rpo sub div empty ++ 域名
/** * 获取顶级域名 * @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; }
标签:substr 防止 min rpo sub div empty ++ 域名
原文地址:https://www.cnblogs.com/-mrl/p/9061483.html