tags:

views:

60

answers:

1

If the url is http://www.google.com/url?sa=t&source=web&ct=res&cd=1&ved=0CAsQFjAA&url=https%3A%2F%2Fwww.google.com%2Fadsense%2F&ei=1AdLS5HSI4yQ6APt6Ly-BQ&usg=AFQjCNHKn8TzGhRO1eUfLhB79AVU-_FnGQ&sig2=EGlbrGQ3jTQdTViEt14cYg,

I need the result to be :google.com

+7  A: 

You can use parse_url to do it like so:

$host = parse_url('http://....', PHP_URL_HOST);
$host_parts = explode('.', $host);
$domain = $host_parts[count($host_parts)-2].'.'.$host_parts[count($host_parts)-1];

That'll do it. Polish it off as you see fit.

Allain Lalonde
What about `google.co.uk`?
Gumbo
You're out of luck. You'd have to write a function that handles all the "interesting" domain suffixes. Or just restrict it to stripping off the www from the front.
Allain Lalonde
Can't you just grab everything between the `http://` part and the first `/`?
DisgruntledGoat
Yes, but that's not what he wants.
Allain Lalonde