0

Possible Duplicate:
PHP REGEX: Get domain from URL

I am working on a project and it need to select only main url such as from http://domain.com/link/project/domain/index.php i need to select only domain.com

Community
  • 1
  • 1
Navneet Pandey
  • 135
  • 1
  • 9

3 Answers3

1

Try $_SERVER['SERVER_NAME'], should give you what you are looking for.

slash197
  • 9,028
  • 6
  • 41
  • 70
1

For a more general use case you can use parse_url:

$url = 'http://domain.com/link/project/domain/index.php';
$parsed = parse_url($url);
var_dump($parsed['host']); // domain.com
var_dump(parse_url($url, PHP_URL_HOST)); // same
cmbuckley
  • 40,217
  • 9
  • 77
  • 91
-1
$myurl = "http://domain.com/link/project/domain/index.php"; 

    // get domain name 
    preg_match("/^(http[s]?:\/\/)?([^\/]+)/i", $myurl, $domain_only); 
    $host = $domain_only[2]; 
    echo "Domain Name is:  " . $host;  
Kaii
  • 20,122
  • 3
  • 38
  • 60
CsaByte
  • 724
  • 5
  • 7