Jump to content

Split domain with more then one subdomains


Mutatos

Recommended Posts

Hi I have an regular expression for splitting into array elements given domain name with subdomain. But my expression does not work with more then one subdomain. I would like to get every subdomain as array element.

 

Here the regular expression:

$regex = "/^([a-z0-9]+\.)*([a-z0-9]{4,}\.(([a-z]{2,5})|([a-z]{2,3}\.[a-z]{2})))$/i";

 

it works for:

domain.com

c.domain.com

 

but does not works with:

b.c.domain.com

a.b.c.domain.com

 

Thanks in advice,

 

Cheers

Nik

Link to comment
Share on other sites

I have try with this

 

$regex = "/^(([a-z0-9][a-z0-9]+?)\.|([a-z0-9]+)\.)*([a-z0-9]{4,}\.(([a-z]{2,5})|([a-z]{2,3}\.[a-z]{2})))$/i";

 

with the domain: sub.a.domain.com

 

and as result I get:

array

  0 => string 'sub.a.domain.com' (length=16)

  1 => string 'a.' (length=2)

  2 => string 'sub' (length=3)

  3 => string 'a' (length=1)

  4 => string 'domain.com' (length=10)

  5 => string 'com' (length=3)

  6 => string 'com' (length=3)

 

This is ok for me, but the problem is when I try with the domain: b.c.domain.com,

 

then it does not works ... I get this:

array

  0 => string 'b.c.domain.com' (length=14)

  1 => string 'c.' (length=2)

  2 => string '' (length=0)

  3 => string 'c' (length=1)

  4 => string 'domain.com' (length=10)

  5 => string 'com' (length=3)

  6 => string 'com' (length=3)

 

How can I parse the domain b.c.domain.com right, like the first one?

Link to comment
Share on other sites

str replace the www. and http:// with nothing, then explode it by the periods.

 

$a = 'http://www.a.b.c.domain.com';

$a = str_replace("www.", "", str_replace("http://", "", $a));

$DA = explode('.', $a);

 

Then the array will have these contents:

 

$DA[0] = 'a';

$DA[1] = 'b';

$DA[2] = 'c';

$DA[3] = 'domain';

$DA[4] = 'com';

 

So... the last element is the domain extension, the second to last is the domain itself, and any preceding elements in the array are subdomains.  count($Array) can give you number of elements.

Link to comment
Share on other sites

Well the explode is not so elegant solution, because the number of elements in your array will be in different cases not the same and this can bring errors.

 

Here is an regex, that for now works for me. Has anyone any better solution?

 

$regex = "/^(([0-9a-zA-Z\-\_]+\.)+([0-9a-zA-Z\-\_]+\.)|([0-9a-zA-Z\-\_]+\.))*([a-z0-9]{4,}\.(([a-z]{2,5})|([a-z]{2,3}\.[a-z]{2})))$/i";

 

The advantage here is, that the number of the array elements the same is:

 

array

  0 => string 'domain.com' (length=10)

  1 => string '' (length=0)

  2 => string '' (length=0)

  3 => string '' (length=0)

  4 => string '' (length=0)

  5 => string 'domain.com' (length=10)

  6 => string 'com' (length=3)

  7 => string 'com' (length=3)

 

array

  0 => string 'test.domain.com' (length=15)

  1 => string 'test.' (length=5)

  2 => string '' (length=0)

  3 => string '' (length=0)

  4 => string 'test.' (length=5)

  5 => string 'domain.com' (length=10)

  6 => string 'com' (length=3)

  7 => string 'com' (length=3)

 

array

  0 => string 'b.c.domain.com' (length=14)

  1 => string 'b.c.' (length=4)

  2 => string 'b.' (length=2)

  3 => string 'c.' (length=2)

  4 => string '' (length=0)

  5 => string 'domain.com' (length=10)

  6 => string 'com' (length=3)

  7 => string 'com' (length=3)

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.