Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Easy Question: Character Loop

    What's the best way to have PHP generate a loop that outputs every combination of the alphabet letters given a maximum length.

    Ex. Say the max is 5

    Output: A B C.... ZZZZZ

    Lepoor

  • #2
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    for ($x='A'strlen($x)<=5$x++)
        print 
    $x.' '

  • #3
    Regular Coder
    Join Date
    Sep 2005
    Posts
    394
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Nice code fci. It did however time out for me, as it took more than 30 secs to execute. Would be rather heavy on the server, so use it wisely.

    ~Phil~

  • #4
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    took me about a minute to run it into a file
    PHP Code:
    <?php

    print date('r').'<br />';
    $fp fopen('abc.txt''wb+');
    for (
    $x='A'strlen($x)<=5$x++)
        
    fwrite($fp$x."\r\n");
    fclose($fp);
    print 
    date('r').'<br />';

    /*
    Mon, 19 Jun 2006 14:19:40 -0500
    Mon, 19 Jun 2006 14:20:36 -0500
    */
    ?>


    ...creating bigger chunks makes it take less time though:
    PHP Code:
    <?php

    print date('r').'<br />';
    $fp  fopen('abc.txt''wb+');
    $mod 1000;
    $tmp '';
    for (
    $z='',$x='A'strlen($x)<=5$x++, $z++) {
        if (
    $z%$mod==0) {
            
    fwrite($fp$tmp);
            
    $tmp ='';
        } else { 
            
    $tmp .= "$x\r\n";
        }
    }
    if (
    $z%$mod==0)
        
    fwrite($fp$tmp);

    fclose($fp);
    print 
    date('r').'<br />';

    /*
    Mon, 19 Jun 2006 14:27:29 -0500
    Mon, 19 Jun 2006 14:27:50 -0500
    */
    ?>

  • #5
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    I really wonder what this might be for... If you use the 26 character set with length 5 you get 11 881 376 combinations (quite a bunch) with ten it's ~1,4116 * 10^14... say you do a milion combinations per second (fci's faster attempt did ~550 000/second)... and it takes you ~4,5years.
    Do you still wonder why it timed out lavinpj1?
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #6
    Regular Coder
    Join Date
    Sep 2005
    Posts
    394
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I didn't WONDER why it timed out. I merely pointed out that it should be used wisely due to the high server load.

    ~Phil~

  • #7
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb

    I am interested to get similar results.

    But I want all combinations of the $Char up to 5 char.

    Code:
    $Char  = 'abcdefghijklmnopqrstuvwxyz';
    $Char .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $Char .= '`~!@#$%^&*()_-+=[]\|}{;:\'",./<>?';
    Any help please?
    Quote Originally Posted by fci View Post
    PHP Code:
    for ($x='A'strlen($x)<=5$x++)
        print 
    $x.' '
    Last edited by nirali35; 04-09-2007 at 05:10 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •