Which command will extract the domain suffix php?

I need to extract the domain name out of a string which could be anything. Such as:

$sitelink="//www.somewebsite.com/product/3749875/info/overview.html";

or

$sitelink="//subdomain.somewebsite.com/blah/blah/whatever.php";

In any case, I'm looking to extract the 'somewebsite.com' portion [which could be anything], and discard the rest.

j0k

22.3k28 gold badges77 silver badges86 bronze badges

asked Feb 7, 2013 at 7:01

4

With parse_url[$url]



The above example will output:

Array
[
    [scheme] => http
    [host] => hostname
    [user] => username
    [pass] => password
    [path] => /path
    [query] => arg=value
    [fragment] => anchor
]

Using thos values

echo parse_url[$url, PHP_URL_HOST]; //hostname

or

$url_info = parse_url[$url];
echo $url_info['host'];//hostname

4

2 complexe url

$url="//www.example.co.uk/page/section/younameit";
or
$url="//example.co.uk/page/section/younameit";

To get "www.example.co.uk":

$host=parse_url[$url, PHP_URL_HOST];

To get "example.co.uk" only

$parts = explode['www.',$host];
$domain = $parts[1];

// ...or...

$domain = ltrim[$host, 'www.']

If your url includes "www." or not you get the same end result, i.e. "example.co.uk"

Voilà!

Jabari

5,1893 gold badges25 silver badges32 bronze badges

answered Apr 23, 2014 at 1:25

You need package that uses Public Suffix List, only in this way you can correctly extract domains with two-, third-level TLDs [co.uk, a.bg, b.bg, etc.] and multilevel subdomains. Regex, parse_url[] or string functions will never produce absolutely correct result.

I recomend use TLD Extract. Here example of code:

$extract = new LayerShifter\TLDExtract\Extract[];

$result = $extract->parse['//www.somewebsite.com/product/3749875/info/overview.html'];
$result->getSubdomain[]; // will return [string] 'www'
$result->getHostname[]; // will return [string] 'somewebsite'
$result->getSuffix[]; // will return [string] 'com'
$result->getRegistrableDomain[]; // will return [string] 'somewebsite.com'

answered Jul 1, 2016 at 14:49

For a string that could be anything, new approach:

function extract_plain_domain[$text] {

    $text=trim[$text,"/"];
    $text=strtolower[$text];

    $parts=explode["/",$text];
    if [substr_count[$parts[0],"http"]] {
        $parts[0]="";
    }
    reset [$parts];while [list [$key, $val] = each [$parts]] {
            if [!empty[$val]] { $text=$val; break; }
    }

    $parts=explode[".",$text];
    if [empty[$parts[2]]] {
        return $parts[0].".".$parts[1];
        } else {
        $num_parts=count[$parts];
        return $parts[$num_parts-2].".".$parts[$num_parts-1];
        }

} // end function extract_plain_domain

answered Mar 27, 2017 at 11:07

RafaRafa

7827 silver badges8 bronze badges

You can use the Utopia Domains library [//github.com/utopia-php/domains], it will return the domain TLD and public suffix based on Mozilla public suffix list [//publicsuffix.org], it can be used as an alternative to the currently archived TLDExtract package.

You can use 'parse_url' function to get the hostname from your URL and than use Utopia Domains parser to get the correct TLD and join it together with the domain name:

Chủ Đề