How do you strip out the domain name from a URL in php?

后端 未结 9 1410
遥遥无期
遥遥无期 2020-12-02 22:12

Im looking for a method (or function) to strip out the domain.ext part of any URL thats fed into the function. The domain extension can be anything (.com, .co.uk, .nl, .what

9条回答
  •  误落风尘
    2020-12-02 22:57

    There is only one correct way to extract domain parts, it's use Public Suffix List (database of TLDs). I recomend TLDExtract package, here is sample code:

    $extract = new LayerShifter\TLDExtract\Extract();
    
    $result = $extract->parse('www.domain.com/path/script.php?=whatever');
    $result->getSubdomain(); // will return (string) 'www'
    $result->getHostname(); // will return (string) 'domain'
    $result->getSuffix(); // will return (string) 'com'
    

提交回复
热议问题