I want to get a domain name from a url but not the host domain. I'm looking to get the second in the example below so I'm looking for thisdomain.com

http://www.test.com?url=http://www.thisdomain.com/page01.html

Normally I'd use something like parse_url($url, PHP_URL_HOST) if I wanted test.com but if I use parse_url and [query] to try and get the second domain I get url=http://www.thisdomain.com/page01.html

Any ideas on what's the best way to do this?

    Sorry. I can't edit that post to code the link. This is the example.

    http://www.test.com?url=http://www.thisdomain.com/page01.html

    Just to clarify test.com will never change but thisdomain.com will change constantly which is what I want.

      for what its worth:

      $url = 'http://www.test.com?url=http://www.thisdomain.com/page01.html';
      $k=parse_url($url);
      $x=preg_match("#//(.*)/#",$k[path],$m);
      $domain=$m[1];
      echo $domain;
      

      bound to be a smarter answer

        Or:

        $parts = explode('?', $str);
        parse_str($parts[1], $query);
        echo parse_url($query['url'], PHP_URL_HOST);

          Thanks so much for the fast replies. Just one quick question dagon. It works perfectly and get's www.thisdomain.com but if the url=http://thisdomain.com/pages/page01.html without the www I get thisdomain.com/pages/

          How would I edit your regex "#//(.*)/#" to work for both options? That's with and without www.

            $x=preg_match("#//(.*?)/#",$k[path],$m);
            

              That's perfect. Thanks so much for all your help. Hope I didn't annoy you too much 🙂

                Write a Reply...