Generating unknown depth of nested for loops?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xaxis
    New Member
    • Feb 2009
    • 15

    Generating unknown depth of nested for loops?

    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:

    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;
    }
    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.
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    Not sure, but 10 characters will be a possible combination of over 3.6x10^15 (3,600,000,000, 000,000... Not advised. Unfortunately for loops is all I can think of, but I will keep the juices flowing and come back if I think of anything.

    Comment

    • xaxis
      New Member
      • Feb 2009
      • 15

      #3
      Originally posted by TheServant
      Not sure, but 10 characters will be a possible combination of over 3.6x10^15 (3,600,000,000, 000,000... Not advised. Unfortunately for loops is all I can think of, but I will keep the juices flowing and come back if I think of anything.
      You're absolutely right... I realized that I'm going to need to add a bit more intelligence when it comes to the generation of domains, such as dictionary list scrambles, common domain pre- and -post fixes, none the less I still have not been able to devise a way in which a recursive function can produce the same type of linear output that the plain old nested for loops do.... It's driving me semi-mad.

      My end purpose for this entire project is rather trivial/silly... I want to make a crawler that does no more than sample hex colors from as many websites as possible so I can have a little statistical fun and determine the "color of the web"...

      Thanks for the pick me up.

      Comment

      • TheServant
        Recognized Expert Top Contributor
        • Feb 2008
        • 1168

        #4
        Interesting idea. I still cannot think of any other way besides systematically looping through each possible combination. I hope you have a dedicated server because running something like that could definitely get you banned from a shared one!

        Comment

        • xaxis
          New Member
          • Feb 2009
          • 15

          #5
          You are most correct, a dedicated server is paramount. However I fully intend to make a "polite" crawler bot. Also, I to have not had any luck with such an algorithm, but it's okay because I realized I only need to produce a vast list of domains one time, and after that it will just be referenced in a database... So anyhow, thank you for your thoughts.

          Comment

          • TheServant
            Recognized Expert Top Contributor
            • Feb 2008
            • 1168

            #6
            No worries. Sorry I couldn't have been of any assistance.

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              Originally posted by xaxis
              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.
              I was playing around with this a bit. Ended up with this:
              [code=php]
              <?php
              $chars = "abc"; // etc...
              $end = ".com"; // To be appended to each line

              function Generate($depth , &$output, $prefix="")
              {
              global $chars;
              global $end;

              for($i = 0; $i < strlen($chars); $i++)
              {
              $currentPrefix = $prefix . $chars[$i];
              if($depth > 1) {
              Generate($depth - 1, $output, $currentPrefix) ;
              }
              else {
              $output .= $currentPrefix . $end . PHP_EOL;
              }
              }
              }

              Generate(2, $out);
              echo $out;
              ?>
              [/code]
              It should give you all possible combinations of the characters in the $chars string using the depth you specify.

              The idea is that every level of recursion, until the bottom level, loops through all the characters and passes each one to the next level, passing along the characters of it's parent loop, and the bottom loop prints the entire string.
              Thus, printing every possible combination of the chars as deep as you specify.

              Seeing as this uses a single string to store all the output, this script eats up a lot of resources. I would recommend outputting it to a file or a database if you intend to run anything like this at any real depth.

              Comment

              • xaxis
                New Member
                • Feb 2009
                • 15

                #8
                Bravo! \(^_^)/

                Using the $chars string of desired characters as the depth level specifier in and of itself is quite ingenious!

                Thank you very much this will save me from copying and pasting many different for loops.

                Comment

                • TheServant
                  Recognized Expert Top Contributor
                  • Feb 2008
                  • 1168

                  #9
                  Very impressed Atli! Nice work.

                  Comment

                  • Markus
                    Recognized Expert Expert
                    • Jun 2007
                    • 6092

                    #10
                    That's cool - I was unaware you could access a string like an array.

                    :)

                    Comment

                    Working...