0

Possible Duplicate:
Parsing Domain From URL In PHP

how do i get

http://localhost/

from

http://localhost/something/

using php

I tried

    $base  = strtok($url, '/');
Community
  • 1
  • 1
Matt Elhotiby
  • 43,028
  • 85
  • 218
  • 321
  • 4
    which of http://stackoverflow.com/search?q=parse+url+php have you tried and why didnt they solve your question? – Gordon Apr 11 '11 at 16:46

3 Answers3

6

You can use parse_url to get down to the hostname.

e.g.:

$url = "http://localhost/path/to/?here=there";
$data = parse_url($url);
var_dump($data);

/*
Array
(
    [scheme] => http
    [host] => localhost
    [path] => /path/to/
    [query] => here=there
)
*/
Majid Fouladpour
  • 29,356
  • 21
  • 76
  • 127
halfdan
  • 33,545
  • 8
  • 78
  • 87
5
$url = 'http://localhost/something/';
$parsedurl  = parse_url($url);
echo $parsedurl['scheme'].'://'.$parsedurl['host'];
gmadd
  • 1,146
  • 9
  • 18
1

Check out parse_url() - http://php.net/parse_url

Jason McCreary
  • 71,546
  • 23
  • 135
  • 174