PHP从url中删除域名

PHP从url中删除域名,php,url,dns,Php,Url,Dns,我知道网络上有很多关于这个主题的信息,但我似乎无法以我想要的方式理解它 我正在尝试构建一个从url中删除域名的函数: http://blabla.com blabla www.blabla.net blabla http://www.blabla.eu blabla 只需要域的普通名称 使用parse_url,我可以过滤域,但这还不够。 我有3个函数可以触发域,但仍然得到一些错误的输出 function prepare_array($domains) { $prep_

我知道网络上有很多关于这个主题的信息,但我似乎无法以我想要的方式理解它

我正在尝试构建一个从url中删除域名的函数:

http://blabla.com    blabla
www.blabla.net       blabla
http://www.blabla.eu blabla
只需要域的普通名称

使用parse_url,我可以过滤域,但这还不够。 我有3个函数可以触发域,但仍然得到一些错误的输出

function prepare_array($domains)
{
    $prep_domains = explode("\n", str_replace("\r", "", $domains)); 
    $domain_array = array_map('trim', $prep_domains); 

    return $domain_array;
}

function test($domain) 
{
    $domain = explode(".", $domain);
    return $domain[1];
}

function strip($url) 
{ 
   $url = trim($url);
   $url = preg_replace("/^(http:\/\/)*(www.)*/is", "", $url); 
   $url = preg_replace("/\/.*$/is" , "" ,$url); 
   return $url; 
}
允许所有可能的域、url和扩展。函数完成后,它必须返回一个仅包含域名本身的数组

更新: 谢谢你的建议

在你们的帮助下我找到了答案

function test($url) 
{   
    // Check if the url begins with http:// www. or both
    // If so, replace it
    if (preg_match("/^(http:\/\/|www.)/i", $url))
    {
        $domain = preg_replace("/^(http:\/\/)*(www.)*/is", "", $url);
    }
    else
    {
        $domain = $url;
    }

    // Now all thats left is the domain and the extension
    // Only return the needed first part without the extension    
    $domain = explode(".", $domain);

    return $domain[0];
}
怎么样

$wsArray = explode(".",$domain); //Break it up into an array. 
$extension = array_pop($wsArray); //Get the Extension (last entry)
$domain = array_pop($wsArray); // Get the domain

啊,您的问题在于TLD可以分为一个或两个部分,例如.com和.co.uk

我要做的是维护TLD列表。解析url后的结果,检查列表并查找匹配项。去掉TLD,在“.”上分解,最后一部分将采用您想要的格式

这似乎没有它可能的那么有效,但是,随着TLD一直被添加,我看不到任何其他确定的方式。

尝试使用preg\u replace

差不多 $domain=preg_replace($regex,$1',$url)


好的……这很麻烦,您应该花一些时间优化和缓存以前派生的域。您还应该有一个友好的名称服务器,最后一个问题是域必须在其DNS中有一个“a”记录

这将尝试以相反的顺序组装域名,直到它能够解析为DNS“a”记录

无论如何,这让我感到困扰,所以我希望这个答案能有所帮助:

<?php
$wsHostNames = array(
    "test.com",
    "http://www.bbc.com/news/uk-34276525",
    "google.uk.co"
);
foreach ($wsHostNames as $hostName) {
    echo "checking $hostName" . PHP_EOL;
    $wsWork = $hostName;
    //attempt to strip out full paths to just host
    $wsWork = parse_url($hostName, PHP_URL_HOST);
    if ($wsWork != "") {
        echo "Was able to cleanup $wsWork" . PHP_EOL;
        $hostName = $wsWork;
    } else {
        //Probably had no path info or malformed URL
        //Try to check it anyway
        echo "No path to strip from $hostName" . PHP_EOL;
    }

    $wsArray = explode(".", $hostName); //Break it up into an array.

    $wsHostName = "";
    //Build domain one segment a time probably
    //Code should be modified not to check for the first segment (.com)
    while (!empty($wsArray)) {
        $newSegment = array_pop($wsArray);
        $wsHostName = $newSegment . $wsHostName;
        echo "Checking $wsHostName" . PHP_EOL;
        if (checkdnsrr($wsHostName, "A")) {
            echo "host found $wsHostName" . PHP_EOL;
            echo "Domain is $newSegment" . PHP_EOL;
            continue(2);
        } else {
            //This segment didn't resolve - keep building
            echo "No Valid A Record for $wsHostName" . PHP_EOL;
            $wsHostName = "." . $wsHostName;
        }
    }
    //if you get to here in the loop it could not resolve the host name

}
?>

尝试使用
parse_url
函数执行此操作。子域呢?事实上,上面的ChoiZ评论可能是更好的答案。不幸的是,这不起作用。当我输入时,它返回我昨晚给出的新解决方案。我只是想从你的评论中确定一下。它最初看起来像是您试图从任何tld中提取名称“blabla”。但现在你说如果你输入“google.com”,它只会给你谷歌。这不是您要查找的结果吗?Stackoverflow更改了原始内容:“google.com”,仍然不是我键入的输出。这没有回答问题,因为链接中提供的正则表达式没有任何捕获组。
function test($url) 
{   
    // Check if the url begins with http:// www. or both
    // If so, replace it
    if (preg_match("/^(http:\/\/|www.)/i", $url))
    {
        $domain = preg_replace("/^(http:\/\/)*(www.)*/is", "", $url);
    }
    else
    {
        $domain = $url;
    }

    // Now all thats left is the domain and the extension
    // Only return the needed first part without the extension    
    $domain = explode(".", $domain);

    return $domain[0];
}