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.
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.
works for me on
$url = 'http://www.test.com?url=http://www.thisdomain.com/page01.html'; and $url = 'http://www.test.com?url=http://thisdomain.com/page01.html';
results: www.thisdomain.com thisdomain.com
it would fail with out the http://
ya that's right both thoes examples return
www.thisdomain.com
The problem is when it's
$url = 'http://www.test.com?url=http://www.thisdomain.com/blog/august/page01.html';
Then it returns
www.thisdomain.com/blog/august
I don't want it to display the path if you follow me. Just the domain. Any idea how I'd modify the regex to prevent the path being added?
$x=preg_match("#//(.*?)/#",$k[path],$m);
That's perfect. Thanks so much for all your help. Hope I didn't annoy you too much 🙂