Mutatos Posted September 21, 2009 Share Posted September 21, 2009 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 Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted September 21, 2009 Share Posted September 21, 2009 you may have to do a loop... so like grab the URL and explode it on the . (dot) and count the array and then do a for loop that loops through the results and within the loop do your regex Quote Link to comment Share on other sites More sharing options...
Mutatos Posted September 21, 2009 Author Share Posted September 21, 2009 Hmm, Why loop? Is it not posible with regex? If we made it only for two subdomains? Should it work without loop? Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted September 21, 2009 Share Posted September 21, 2009 well the reason I say loop is cause the way you have your regex (or at least the way I am reading it) is only accounting for subdomains that 3 domain parts (a.b.com) and not any greater than that. Quote Link to comment Share on other sites More sharing options...
Mutatos Posted September 21, 2009 Author Share Posted September 21, 2009 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? Quote Link to comment Share on other sites More sharing options...
5kyy8lu3 Posted September 21, 2009 Share Posted September 21, 2009 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. Quote Link to comment Share on other sites More sharing options...
Mutatos Posted September 22, 2009 Author Share Posted September 22, 2009 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) Quote Link to comment Share on other sites More sharing options...
Zane Posted September 22, 2009 Share Posted September 22, 2009 have you looked at parse_url. The PHP function made for splitting up these kinds of things Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.