Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get domain from URL

Tags:

url

php

OK, before you say "oh, come on! this is easy", I must inform you that I've been testing many many different methods for that specific thing, for a long time, and I haven't found any that really works for any url, and any domain.

Examples :

  • http://www.this-is-a-url.com = this-is-a-url.com
  • www.this-is-another.url.com/some-folder = this-is-another-url.com
  • subdomain.somesub.domain.com/index.php = domain.com
  • diff.erentltd.in = erentltd.in
  • www.andanotherone.org.uk = andanotherone.org.uk

So, any ideas? Do you know of any working function/script?


To anyone interested : Please have a look at @bystwn22's answer. It's one of the smoothest working solutions you could possibly find! :-)

like image 673
Dr.Kameleon Avatar asked Jul 17 '13 10:07

Dr.Kameleon


People also ask

How do I find the domain of a string?

First let's create a string with our URL (Note: If the URL isn't correctly structured you'll get an error). const url = 'https://www.michaelburrows.xyz/blog?search=hello&world'; Next we create a URL object using the new URL() constructor. let domain = (new URL(url));

How do I extract a domain from a URL in Google Sheets?

The =REGEXREPLACE() function is built-in Google Sheets and it extracts domains from URLs. What's great about is it's only a simple line of code that you can paste into your cell. The function is not super technical and you can change it any way you see fit.


2 Answers

Okay try this, i know the question is really tricky :\

<?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"
?>

Function get_domain

<?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 ) );
      }
    }
  }
?>
like image 122
bystwn22 Avatar answered Oct 28 '22 23:10

bystwn22


I worked on a simpler solution. Due to the issues we faced with parse_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 ));
}
like image 42
DevZer0 Avatar answered Oct 28 '22 22:10

DevZer0