Page 1 of 2

Getting domain from URL

Posted: Sun Mar 14, 2004 7:37 pm
by Pyrite
Just wanting to get just the real domain (not subdomains) out of a URL. Or from another place if there is one. But like, have a webmail login form, and want users to input just their username, and then my script will fill in the domain from the url.

So like given:

http://www.yahoo.com
news.yahoo.com
us2.photos.yahoo.com
yahoo.com

, code will always grab just yahoo.com ?

I've searched and can't find any simple solutions on this ..anyone?

Posted: Sun Mar 14, 2004 11:35 pm
by infolock
maybe something like this would work :

Code: Select all

$url = 'www.yahoo.com';
$url_dump = explode(".",$url);

$i = count($url_dump) - 1;
$b = count($url_dump);
echo 'the domain is '.$url_dump[$i].'.'.$url_dump[$b];
simple, but effective.

Posted: Sun Mar 14, 2004 11:49 pm
by Pyrite
For me, that code prints:

"the domain is com." :roll:

Posted: Sun Mar 14, 2004 11:55 pm
by Pyrite

Code: Select all

$url = 'www.news.yahoo.com';
$url_dump = explode(".",$url);
$i = count($url_dump) - 2;
$b = count($url_dump) - 1;
echo 'the domain is '.$url_dumpї$i].'.'.$url_dumpї$b];
but this however, appears to work. :D

Posted: Mon Mar 15, 2004 1:58 am
by infolock
oops mybad heh. didn't exactly test the bugger so guess it probably needed some tweakin 8) . glad ya got it though.

Posted: Mon Mar 15, 2004 3:03 am
by JayBird
Also, take a look at the parse_url function

http://se.php.net/manual/en/function.parse-url.php

Mark

Posted: Mon Mar 15, 2004 9:24 am
by Pyrite
I did, and infolock's explode method works better :P

Posted: Mon Mar 15, 2004 9:40 am
by JayBird
well, bully for you! :P

:)

Mark

Posted: Mon Mar 15, 2004 6:30 pm
by Pyrite
lol

Posted: Mon Mar 15, 2004 6:35 pm
by tim
what if the domain was:

http://www.test.co.uk

your method would only print co.uk

just another glitch to think about

Posted: Mon Mar 15, 2004 10:14 pm
by infolock
ew... good point. i'm sure there might be a library of possible domain name extensions that you could use as a guide.. If (count($url_dump) - 1 . count($url_dump) - 2 == $this_extension) then $url_dump -2, $url_dump -3.

no, that isn't exaclty right, but ya get the picture. might would work.. might.. damn you tim!@# :twisted:

Posted: Tue Mar 16, 2004 8:00 am
by tim
infolock wrote: damn you tim!@# :twisted:
sorry, anyone from Kentucky is a "must" to give a hard time. my uncle lives there and is always riding me. lol

Posted: Tue Mar 16, 2004 4:53 pm
by infolock
haha, glad to know that there are rednecks outside of kentucky

Posted: Tue Mar 16, 2004 8:07 pm
by Pyrite
The only rednecks outside of Kentucky are Kentuckians

Posted: Wed Mar 17, 2004 11:39 am
by infolock
haha, well tim is a buckeye, our arch nemesis, so i guess that's close enough :D