Php 如何使url在没有www的情况下显示主机名?

Php 如何使url在没有www的情况下显示主机名?,php,Php,如何使url在没有www的情况下显示主机名 如果URL是 怎么做到这样 谷歌网站 我的代码 $rgx25 = '/\<a class\=\"live\" rel\=\"nofollow\" href\=\"(|https:\/\/|http:\/\/)(|www\.)(.*?)\" target\=\"\_blank\"\>(.*?)\<\/a\>/iu'; if (preg_match_all($rgx25, $story['text'], $matches)) {

如何使url在没有www的情况下显示主机名

如果URL是

怎么做到这样

谷歌网站

我的代码

$rgx25 = '/\<a class\=\"live\" rel\=\"nofollow\" href\=\"(|https:\/\/|http:\/\/)(|www\.)(.*?)\" target\=\"\_blank\"\>(.*?)\<\/a\>/iu';
if (preg_match_all($rgx25, $story['text'], $matches))
{
    foreach ($matches[0] as $k => $match)
    {
        $url = $matches[1][$k] . $matches[2][$k] . $matches[3][$k];
    }
}
$rgx25='/\(.*)\/iu';
if(preg_match_all($rgx25,$story['text',$matches))
{
foreach($k=>$match时匹配[0]
{
$url=$matches[1][$k].$matches[2][$k].$matches[3][$k];
}
}
$url='1!'http://sub.example.com/path?googleguy=googley';
$test=parse_url($url);
$string=explode('.',$test[“host”]);
$top_lev_dom=数组(“com”、“net”、“us”、“gov”、“io”、“xyz”、“org”、“int”、“edu”);
美元产出;
$end=false;
$pos=0;
$2;
对于($i=0;$i
example.com


您可以使用PHP的
parse_url()
函数来获得url部分的关联数组:。如何让url通过PHP仅显示主域?修复答案请给我几分钟,如果域是example.com/path?googleguy=googley so to ple.combut example.com来组合,因为它需要https://或www.infront,或者您希望它也被过滤?和example.com
$url = 'http://sub.example.com/path?googleguy=googley';
$test = parse_url($url);
$string = explode('.',$test["host"]);
$top_lev_dom = array("com","net","us","gov","io","xyz","org","int","edu");
$output;
$end = false;
$pos = 0;
$pos2;
for($i = 0; $i < count($string); $i++){
    for($x = 0; $x < count(top_lev_dom); $x++){
        if($string[$i]==$top_lev_dom[$x]){
            $pos = $i;
            $pos2 = $i - 1;
            $end = true;
            break;
        }
        if($end == true){break;}
    }
    if($end == true){break;}
}

if($pos==0){
    $output = "error";
}else{
    $output.=$string[$pos2].".".$string[$pos];
}

echo $output;