【问题标题】:PHP Strip domain name from urlPHP 从 url 中去除域名
【发布时间】:2015-12-14 18:58:49
【问题描述】:

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

我正在尝试构建一个从 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];
}

【问题讨论】:

标签: php url dns


【解决方案1】:

怎么样

$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

【讨论】:

  • 其实上面的 ChoiZ 评论可能是更好的答案。
  • 不幸的是,这不起作用。当我输入google.com 时,它返回google
  • 我昨晚给出了一个新的解决方案。我只是想从你的评论中确定。最初看起来您正试图从任何顶级域名中提取名称“blablabla”。但是现在你说如果你输入“google.com”它只会给你谷歌。这不是你想要的结果吗?
  • Stackoverflow 更改了原来的内容是:“google.com”“google.com”,仍然不是我输入的输出
【解决方案2】:

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

我要做的是维护一个 TLD 列表。使用 parse_url 之后的结果,查看列表并查找匹配项。去掉 TLD,在 '.' 上爆炸。最后一部分将采用您想要的格式。

这似乎没有尽可能高效,但是随着 TLD 一直在添加,我看不到任何其他确定性方式。

【讨论】:

    【解决方案3】:

    尝试使用 preg_replace。

    类似的东西 $domain = preg_replace($regex, '$1', $url);

    regex

    【讨论】:

    • 这没有回答问题,因为链接中提供的正则表达式没有任何捕获组。
    【解决方案4】:

    好的...这很混乱,您应该花一些时间优化和缓存以前派生的域。您还应该有一个友好的 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
    
    }
    ?>
    

    【讨论】:

      【解决方案5】:
      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];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-31
        • 1970-01-01
        • 2017-12-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多