Getting domain from URL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Getting domain from URL

Post 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?
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post 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.
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

For me, that code prints:

"the domain is com." :roll:
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post 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
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

oops mybad heh. didn't exactly test the bugger so guess it probably needed some tweakin 8) . glad ya got it though.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Also, take a look at the parse_url function

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

Mark
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

I did, and infolock's explode method works better :P
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

well, bully for you! :P

:)

Mark
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

lol
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post 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:
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

haha, glad to know that there are rednecks outside of kentucky
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

The only rednecks outside of Kentucky are Kentuckians
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

haha, well tim is a buckeye, our arch nemesis, so i guess that's close enough :D
Post Reply