1

Possible Duplicate:
Parsing Domain From URL In PHP
How do I parse a URL in PHP?

I have this code:

$url = preg_replace("/^(http:\/\/)*(www.)*/is", "", $url);
    $url = preg_replace("/\/.*$/is" , "" ,$url);

It work good get domain name where after .com is / . I got problem if there is url like this:

http://www.something.com?id=21213

What do I need to add to regex to cut ?id=21213 so there remains only something.com

Community
  • 1
  • 1
  • What exactly do you require? Given a url www.something.com/?id=123 which part of it do you want? – GarethL Oct 16 '12 at 15:30
  • possible duplicate of [how do i parse url php](http://stackoverflow.com/questions/5624370/how-do-i-parse-url-php); [Parsing Domain from URL](http://stackoverflow.com/questions/276516/parsing-domain-from-url-in-php) – hakre Oct 16 '12 at 15:32
  • -1: Didn't bother to look in the manual. – hakre Oct 16 '12 at 15:33
  • GarethL - cut ?id=21213 so there remains only something.com hakre- parse_url() is not option. – Daisy de Melker Oct 16 '12 at 15:34

1 Answers1

2

See the magical build in function of parse_url().

Using parse_url(), you can retrieve the domain name solely by:

$domain = parse_url($url, PHP_URL_HOST);
if(0 === strpos('www.', $domain)){
    $domain = substr($domain, 4);
}
mauris
  • 42,982
  • 15
  • 99
  • 131