Forum Moderators: coopster

Message Too Old, No Replies

Removing subdomains from strings

         

asantos

10:48 am on Jun 23, 2006 (gmt 0)

10+ Year Member



Hi.
I need to get the domain name and the .com/.net... etc.

For example:
foo.com > foo.com
www.foo.com > foo.com
somesubdomain.foo.com > foo.com
one.two.foo.com > foo.com

I need to get the foo.com part in all cases. What regexp/function could i use?

scriptmasterdel

11:49 am on Jun 23, 2006 (gmt 0)

10+ Year Member



This might be the long winded way around, but this is how i would do it


<?php
// define the URL
$url = 'one.two.foo.com';

// break it up using the "."
$urlb = explode('.',$url);

// get the domain
$dns = $urlb[count($urlb)-2];

// get the extension
$ext = $urlb[count($urlb)-1];

put it back together
$fullDomain = $dns.'.'.$ext;
?>

If i find a simpler way il post it here, but this works with several subdomains, try it out =)

Hope i have hepled

Del

Scally_Ally

3:54 pm on Jun 23, 2006 (gmt 0)

10+ Year Member



thats cool and works for one.two.foo.com

What if it is one.two.foo.co.uk? it only displays .co.uk and not foo.co.uk , you need some way of determining if the extension has more than one dot in it..

I think that the only way of doing this is having an array of extensions then searching through each of these to see if there is a match in your string, that way you will know how many dots there should be in the extension.

asantos

5:15 pm on Jun 23, 2006 (gmt 0)

10+ Year Member



scriptmasterdel, thats a nice piece of script, however it wont work in specific cases (mentioned by scally_all).

How could I determine if there are one or more "extensions"? eg.: www.foo.co.uk

and what if is only foo.co.uk and the script returns co.uk only? :s

mcavic

5:40 pm on Jun 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's no way to do it unless you know beforehand how many dots there are in the suffix. You could make a list manually, like
.com
.org
.co.uk
.co.jp

Or possibly do some kind of DNS lookup.

asantos

5:55 pm on Jun 23, 2006 (gmt 0)

10+ Year Member



Mmm teorically i need this to place specific licenses for a CMS. If the user tries to install the software in another domain, it wont work.

I guess ill have to list all the subdomains that the client is going to use.

Thanks again.

mcavic

6:32 pm on Jun 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh, in that case, all you need to do is ask the customer for their domain name, and check that the host name ends with that.

if (preg_match("/[.]*${domain}$/", $host)) {
print "Ok\n";
} else {
print "Not Ok\n";
}

This will accept foo.com or www.foo.com, etc.