Context: I'm working on a function that generates domain names. For instance, if I want to generate every possible domain name with 3 alpha characters, I've been doing something like this:
Now when the time comes that I want to generate all possible domains with say 10 alpha chars, I don't want to have to manually nest that many for loops to achieve the result.
So does anyone have any thoughts on how this might be achieved with the above context in mind. I played with recursion a bit: a function with a single for loop that called itself to a specified $depth, however I could not figure out how to recombine the end $domain parts to get my desired result.
Any help would be most appreciated.
Code:
$alpha = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'); $numeric = array('0','1','2','3','4','5','6','7','8','9'); $condition = 'alpha'; $hyphen = FALSE; switch ($condition) { case 'alpha' : $arr = (!$hyphen) ? $alpha : array_push($alpha, '-'); break; case 'numeric' : $arr = (!$hyphen) ? $numeric : array_push($numeric, '-'); break; case 'alphnum' : $arr = (!$hyphen) ? array_merge($alpha, $numeric) : array_push(array_merge($alpha, $numeric), '-'); break; } generateDomains($arr, 'com'); function generateDomains($arr, $tld) { for ($i=0; $i<count($arr); $i++){ for ($q=0; $q<count($arr); $q++){ for ($p=0; $p<count($arr); $p++){ @$domains .= $arr[$i].$arr[$q].$arr[$p].'.'.$tld.' '; } } } echo $domains; }
So does anyone have any thoughts on how this might be achieved with the above context in mind. I played with recursion a bit: a function with a single for loop that called itself to a specified $depth, however I could not figure out how to recombine the end $domain parts to get my desired result.
Any help would be most appreciated.
Comment