Get domain name (not subdomain) in php

Posted by Cyclone on Stack Overflow See other posts from Stack Overflow or by Cyclone
Published on 2010-04-21T00:36:17Z Indexed on 2010/04/21 0:43 UTC
Read the original article Hit count: 301

Filed under:
|
|
|

I have a URL which can be any of the following formats:

http://example.com
https://example.com
http://example.com/foo
http://example.com/foo/bar
www.example.com
example.com
foo.example.com
www.foo.example.com
foo.bar.example.com
http://foo.bar.example.com/foo/bar
example.net/foo/bar

Essentially, I need to be able to match any normal URL. How can I extract example.com (or .net, whatever the tld happens to be. I need this to work with any TLD.) from all of these via a single regex?

This is in PHP.

Thanks for the help!

© Stack Overflow or respective owner

Related posts about regex

Related posts about php