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
  • 41,184
  • 82
  • 214
  • 317
  • 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

5

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
  • 27,709
  • 19
  • 72
  • 126
halfdan
  • 32,443
  • 8
  • 76
  • 85
5
$url = 'http://localhost/something/';
$parsedurl  = parse_url($url);
echo $parsedurl['scheme'].'://'.$parsedurl['host'];
gmadd
  • 1,136
  • 9
  • 17
1

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

Jason McCreary
  • 69,176
  • 21
  • 125
  • 169