PHP从网址

时间:2015-09-17 19:12:46

标签: php url dns

我知道网上有很多关于这个主题的信息,但我似乎无法按照我想要的方式弄清楚。

我正在尝试构建一个从网址中删除域名的功能:

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; 
}

允许使用每个可能的域名,网址和扩展名。函数完成后,它必须返回一个只有域名本身的数组。

更新: 感谢您的所有建议!

我在大家的帮助下想出来了。

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];
}

5 个答案:

答案 0 :(得分:2)

怎么样

$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

http://php.net/manual/en/function.array-pop.php

答案 1 :(得分:1)

啊,你的问题在于TLD可以是一个或两个部分,例如.com vs .co.uk。

我要做的是维护一份TLD列表。使用parse_url之后的结果,遍历列表并查找匹配项。剥离顶级域名,爆炸'。'最后一部分将采用您想要的格式。

这似乎没有那么高效,但是,随着TLD的不断增加,我看不到任何其他确定性的方式。

答案 2 :(得分:1)

好的......这很乱,你应该花些时间优化和缓存以前派生的域名。您还应该有一个友好的NameServer,最后一个问题是域必须在其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

}
?>

答案 3 :(得分:0)

尝试使用preg_replace。

等等 $ domain = preg_replace($ regex,'$ 1',$ url);

regex

答案 4 :(得分:0)

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];
}