Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从URL获取域_Php_Url - Fatal编程技术网

Php 从URL获取域

Php 从URL获取域,php,url,Php,Url,好的,在你说“哦,来吧!这很简单”之前,我必须告诉你,我已经为这个特定的东西测试了很多不同的方法,很长时间了,我还没有发现任何真正适用于任何url,以及任何域的 示例: =this-is-a-url.com www.this-is-other.url.com/some-folder=this-is-other-url.com subdomain.somesub.domain.com/index.php=domain.com diff.erentltd.in=erentltd.in www.an

好的,在你说“哦,来吧!这很简单”之前,我必须告诉你,我已经为这个特定的东西测试了很多不同的方法,很长时间了,我还没有发现任何真正适用于任何url,以及任何域的

示例:

  • =this-is-a-url.com
  • www.this-is-other.url.com/some-folder=this-is-other-url.com
  • subdomain.somesub.domain.com/index.php=domain.com
  • diff.erentltd.in=erentltd.in
  • www.andanotherone.org.uk=andanotherone.org.uk
有什么想法吗?你知道有什么工作功能/脚本吗



任何感兴趣的人:请看@bystwn22的答案。这是您可能找到的最流畅的工作解决方案之一!:-)

我研究了一个更简单的解决方案。由于我们面临的问题
parse\u url

check("www.google.com");

function check($url) {
        if (!preg_match("/^http/", $url)) $url = "http://" . $url;
        echo preg_replace("/.*\.([^\.]+\.[^\.]+)/", "$1", parse_url ( $url, PHP_URL_HOST ));
}

实际上,您需要两个列表:二级域和顶级域

  • 使用或从您的url获取主机,假设它是subdomain.domain.org.uk

  • 按点分解它,并获取该数组的最后两个元素,再次按点连接(org.uk)。如果这是一个二级域-添加数组的前一个元素,您就拥有了您的域(domain.org.uk)

  • 否则,您的域就是您在步骤2中检查的域(如果数组的最后一个元素是顶级域之一,如果您非常确定该域是有效的,则可以跳过此检查)。如果您的原始主机是subdomain.domain.com,那么您已经检查了domain.com是否为二级域,这意味着domain.com就是您要查找的


  • 这是你的电话号码。或者你可以试着找一个更好的。

    好的,试试这个,我知道这个问题很棘手:\

    <?php
      $urls = array(
        "http://www.this-is-a-url.com",
        "www.this-is-another-url.com/some-folder",
        "subdomain.somesub.domain.com/index.php",
        "diff.erentltd.in",
        "www.andanotherone.org.uk"
      );
    
      foreach( $urls as $url ) {
        var_dump( get_domain( $url ) );
      }
    
      /** Output **/
      // string(17) "this-is-a-url.com"
      // string(23) "this-is-another-url.com"
      // string(10) "domain.com"
      // string(11) "erentltd.in"
      // string(20) "andanotherone.org.uk"
    ?>
    

    如果没有所有有效CTLD的列表,将无法工作。something.com.tk有效,但something.com.at无效。因为com.at不是一个ctld,而是一个普通域。@Rufinus我知道这个问题有多棘手…:你的第二个例子对吗?我认为应该是
    www.this-is-other-url.com/some-folder=这是另一个url.com
    这里有两个有用的链接:而且我认为在事先不知道哪些子域重要与否的情况下,不可能区分
    importantsubdomain.domain.com
    不重要的subdomain.domain.com
    。打折
    www.
    很容易,但几乎任何其他东西都可能很重要。你甚至不能依赖有效的TLD列表以及哪些TLD通常具有三级域,因为域行业中出现了许多非常大的变化。不幸的是,这会产生以下结果:
    this-is-a-url.com
    url.com
    domain.com
    erentld.in
    org.uk
    。是的,我知道它不能处理英国的问题,你的也不能,因为你的逻辑确实是硬编码的。我们需要使用一个有效的tld字典,它才能真正work@AmalMurali不。你要检查的是.uk,但除此之外还有更多的二级域。ukI已经查看了列表,你实际上需要连接最后3个元素(如果分解为3个或更多元素),并执行与最后2个元素相同的检查,因为二级域可能类似于“ecape.school.za”。Tho,它不会改变算法太多。这里有两个有用的链接:谢谢你,我认为
    data.iana.org/TLD/tlds-alpha-by-domain.txt
    中的所有TLD都已经在我的列表中:)伙计,我想你搞定了。(我之所以说“思考”,是因为我仍然无法相信它在工作……哈哈——尽管我已经彻底测试了它)。干得好!非常感谢!:-)
    <?php
      function get_domain( $url ) {
        $regex  = "/^((http|ftp|https):\/\/)?([\w-]+(\.[\w-]+)+)([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-])?$/i";
        if ( !preg_match( $regex, $url, $matches ) ) {
          return false;
        }
        $url    = $matches[3];
        $tlds   = array( 'ac', 'ad', 'ae', 'aero', 'af', 'ag', 'ai', 'al', 'am', 'an', 'ao', 'aq', 'ar', 'arpa', 'as', 'asia', 'at', 'au', 'aw', 'ax', 'az', 'ba', 'bb', 'bd', 'be', 'bf', 'bg', 'bh', 'bi', 'biz', 'bj', 'bm', 'bn', 'bo', 'br', 'bs', 'bt', 'bv', 'bw', 'by', 'bz', 'ca', 'cat', 'cc', 'cd', 'cf', 'cg', 'ch', 'ci', 'ck', 'cl', 'cm', 'cn', 'co', 'com', 'coop', 'cr', 'cu', 'cv', 'cx', 'cy', 'cz', 'de', 'dj', 'dk', 'dm', 'do', 'dz', 'ec', 'edu', 'ee', 'eg', 'er', 'es', 'et', 'eu', 'fi', 'fj', 'fk', 'fm', 'fo', 'fr', 'ga', 'gb', 'gd', 'ge', 'gf', 'gg', 'gh', 'gi', 'gl', 'gm', 'gn', 'gov', 'gp', 'gq', 'gr', 'gs', 'gt', 'gu', 'gw', 'gy', 'hk', 'hm', 'hn', 'hr', 'ht', 'hu', 'id', 'ie', 'il', 'im', 'in', 'info', 'int', 'io', 'iq', 'ir', 'is', 'it', 'je', 'jm', 'jo', 'jobs', 'jp', 'ke', 'kg', 'kh', 'ki', 'km', 'kn', 'kp', 'kr', 'kw', 'ky', 'kz', 'la', 'lb', 'lc', 'li', 'lk', 'lr', 'ls', 'lt', 'lu', 'lv', 'ly', 'ma', 'mc', 'md', 'me', 'mg', 'mh', 'mil', 'mk', 'ml', 'mm', 'mn', 'mo', 'mobi', 'mp', 'mq', 'mr', 'ms', 'mt', 'mu', 'museum', 'mv', 'mw', 'mx', 'my', 'mz', 'na', 'name', 'nc', 'ne', 'net', 'nf', 'ng', 'ni', 'nl', 'no', 'np', 'nr', 'nu', 'nz', 'om', 'org', 'pa', 'pe', 'pf', 'pg', 'ph', 'pk', 'pl', 'pm', 'pn', 'pr', 'pro', 'ps', 'pt', 'pw', 'py', 'qa', 're', 'ro', 'rs', 'ru', 'rw', 'sa', 'sb', 'sc', 'sd', 'se', 'sg', 'sh', 'si', 'sj', 'sk', 'sl', 'sm', 'sn', 'so', 'sr', 'st', 'su', 'sv', 'sy', 'sz', 'tc', 'td', 'tel', 'tf', 'tg', 'th', 'tj', 'tk', 'tl', 'tm', 'tn', 'to', 'tp', 'tr', 'travel', 'tt', 'tv', 'tw', 'tz', 'ua', 'ug', 'uk', 'us', 'uy', 'uz', 'va', 'vc', 've', 'vg', 'vi', 'vn', 'vu', 'wf', 'ws', 'ye', 'yt', 'yu', 'za', 'zm', 'zw' );
        $parts  = array_reverse( explode( ".", $url ) );
        $domain = array();
    
        foreach( $parts as $part ) {
          $domain[] = $part;
          if ( !in_array( strtolower( $part ), $tlds ) ) {
            return implode( ".", array_reverse( $domain ) );
          }
        }
      }
    ?>