所以我有这种代码,它返回域名,但是我不知道如何删除子域,任何人都可以帮忙吗?
$link='http://www.lol.wwwyoursitewww.com/aaaaa/ggghd/site.php?sadf=asg';
preg_match('/^http\:\/\/www.(.*?)\/.*/i', $link, $link_domain);
echo $link_domain[1];
我会使用内置的parse_url来做尽可能多的事情,这只会让您留下域名进行整理。我对要求有些不清楚。什么是预期的输出? -只是wwwyoursitewww.com?或http://wwwyoursitewww.com/aaaaa/ggghd/site.php?sadf=asg
$link='http://www.lol.wwwyoursitewww.com/aaaaa/ggghd/site.php?sadf=asg';
$url = parse_url($link);
if (preg_match("/(www.*?)\.(.*)/", $url['host'], $m)) {
$url['host'] = $m[2];
}
$rebuild = $url['scheme'] . '://' . $url['host'] . $url['path'] . '?' . $url['query'];
echo "$rebuild\n";
$link='http://www.lol.wwwyoursitewww.com/aaaaa/ggghd/site.php?sadf=asg';
preg_match('!www((\.(\w)+))+!', $link, $match);
$link_arr=(explode(".", $match[0]));
echo $link_domain = $link_arr[count($link_arr)-1];
输出:com
获取host
,不包含子域。使用www
/**
* Get Host without subdomain
* @param $host
* @return string
*/
public static function giveHost($host): string
{
$newHost = $host;
$host_array = explode('.', $host);
$countDot = count($host_array);
if ($countDot > 2){
$newHost = $host_array[$countDot-2].'.'.$host_array[$countDot-1];
if (preg_match('/www/', $host)){
$newHost = 'www.'.$newHost;
}
}
return $newHost;
}