parse_url() will give you a lot more than that. Did you care to read the documentation?
Aside from that, removing the www. subdomain is easy. The tricky part will be removing subdomains not part of the desired domain. You will indeed need to specify any top-level extensions you expect to be used... or just guess using the extension length, which wouldn't be entirely trustworthy if you plan on using shorter 2nd-level (cnn.com, go.com) or longer 1st-level (example.travel, example.museum).