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

后端 未结 9 1411
遥遥无期
遥遥无期 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条回答
  •  旧时难觅i
    2020-12-02 22:58

    You can use parse_url() to do this:

    $url = 'http://www.example.com';
    $domain = parse_url($url, PHP_URL_HOST);
    $domain = str_replace('www.','',$domain);
    

    In this example, $domain should contain example.com, irrespective of it having www or not. It also works for a domain such as .co.uk

提交回复
热议问题