Tomasz Tybulewicz
Tomasz Tybulewicz

Reputation: 8647

Extract domain from link

I'm looking for a bullet proof way to extract domain (with tld) from any given link - an eqivalent of dirname($path) let's call it domainname($link).

echo domainname("http://example.com/index.html?a=123%1231");

should print "example.com"

Upvotes: 2

Views: 1751

Answers (2)

Ward Werbrouck
Ward Werbrouck

Reputation: 1462

echo parse_url($url, PHP_URL_HOST);

More info: http://be.php.net/manual/en/function.parse-url.php

Upvotes: 8

SilentGhost
SilentGhost

Reputation: 319531

parse_url return value has a host key.

Upvotes: 6

Related Questions