Web Hosting Talk







View Full Version : Easy PHP code needed (1 liner)


UH-Matt
11-25-2002, 06:06 AM
Ok here is what i need:

$domain
is a variable i have set for a domain

I need some PHP to take this variable and split it at the dot into
$main and $tld

so $domain = bleh.com

will become

$main = bleh
$tld = .com

It also needs to work for tld's with 2 dots.. like .co.uk.

Simple easy code.. anyone care to post the answer, cheers :D

skoop
11-25-2002, 06:58 AM
$domain=eregi_replace("www.", "", $domain);
$exploded=explode(".", $domain);
$main=$explode[0];
$tld=$explode[1];
if($explode[2]!="")
{
$tld.=".".$explode[2];
}


this should work

Rich2k
11-25-2002, 07:00 AM
I guess you could explode it at the dot into an array and then work out the size i.e. the last and second to last (if a .uk or equivalent) would only be a max of 4 or three characters respectively. Thus you could work out which in the array contains the actual domain name.

Probably an easier way of working it out though and that method wouldn't work for ones like hp.com

Also the variable you have, is it JUST the domain name or does it contain subdomains such as www. or sub. for example.

If it's just the domain name then it becomes quite simple e.g.

domain.co.uk

$split = explode(".", $domain);
$main = $split[0];
$arraysize = sizeof($split);
for ($x=1; $x<$arraysize; $x++) {
$tld .= "." . $arraysize[$x];
}


This would also cope with the ability to have tlds with more than two parts (none at the moment but you never know for the future).

RackNine
11-25-2002, 07:06 AM
Originally posted by skoop
this should work
Careful with that, if the user adds "www." to the front it'll offset the whole system.

And be warned, however many times you state "without www." you just know it'll happen :)

Simple fix though, eregi_replace('www.', '', $value); will remove it. Better yet (since some users will have namewww.com) check the first exploded variable for known subdomains, ie:

ftp.
mail.
pop.
smtp.
www.

Sincerely,

-Matt

UH-Matt
11-25-2002, 07:15 AM
It WONT include the www. i have checks in place to remove this from the variable already.

So which code is simplest to use ?

Rich2k
11-25-2002, 07:19 AM
Either, personally I think mine ;) is future proof against domain authorities having three or more parts to their TLD, but I can't really see this happening for a while yet.

MarkIL
11-25-2002, 07:22 AM
$doms = array('example.co.uk','example.org');
foreach($doms as $dom)
{
$s=explode('.',$dom);
$tld=(count($s)==2)?$s[1]:join('.',array_slice($s,1));
$main=$s[0];
// do stuff with $main and $tld
}

// edit: assign to vars instead of printing.

UH-Matt
11-25-2002, 07:26 AM
ok, Seem people have been making them more complicated than they need to be and Rich2k's code should work.. I will try it later and let you know.

Thanks for the help guys :)

skoop
11-25-2002, 04:00 PM
Originally posted by RackNine

Careful with that, if the user adds "www." to the front it'll offset the whole system.

And be warned, however many times you state "without www." you just know it'll happen :)

Simple fix though, eregi_replace('www.', '', $value); will remove it. Better yet (since some users will have namewww.com) check the first exploded variable for known subdomains, ie:

ftp.
mail.
pop.
smtp.
www.

Sincerely,

-Matt

uhm ... first line of code:


$domain=eregi_replace("www.", "", $domain);

check my original message. didn't edit it. it was there ;)

but you're right about people having somethingwww.com ... that might pose problems.

UH-Matt
11-25-2002, 07:55 PM
$split = explode(".", $domain);
$main = $split[0];
$arraysize = sizeof($split);
for ($x=1; $x<$arraysize; $x++) {
$tld .= "." . $arraysize[$x];
}

gives me $tld = .

not good :*(

UH-Matt
11-25-2002, 07:56 PM
$doms = array('example.co.uk','example.org');
foreach($doms as $dom)
{
$s=explode('.',$dom);
$tld=(count($s)==2)?$s[1]:join('.',array_slice($s,1));
$main=$s[0];
// do stuff with $main and $tld
}

gives me .org everytime

UH-Matt
11-25-2002, 07:58 PM
nobody has a solution which will work ? i thought this was easy! :(

Rich2k
11-25-2002, 08:04 PM
Sorry I made a mistake in the for loop is should be

$tld .= "." . $split[$x];

UH-Matt
11-25-2002, 08:10 PM
that works now.. you are my new god Rich2k