Results 1 to 40 of 40
  1. script that can automatically generate all 3 letter combinations?

    Hi guys. I am getting into the domain's business and in order to help myself out , I have found a script that adds the alphabet to the front of a word that i input , and to the end of it as well.
    Code:
    <form action=<?php echo $_SERVER['PHP_SELF'];?> method=post>
    <input type=text name=input />
    
    <input type=submit name=submit value="Submit" />
    </form>
    
    <?php
    
        
    $alphabet = 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");
    $com = ".com";
    for ($i = 0; $i < 26; $i++) {
    
    
        $word = $alphabet[$i].$_POST[input].$com;
        echo "$word <br>";
    
    }
    
    
    ?>
    
    
    <form action=<?php echo $_SERVER['PHP_SELF'];?> method=post>
    <input type=text name=input />
    
    <input type=submit name=submit value="Submit" />
    </form>
    
    <?php
    
        
    $alphabet = 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");
    $com = ".com";
    for ($i = 0; $i < 26; $i++) {
    
    
        $word = $_POST[input].$alphabet[$i].$com;
        echo "$word <br>";
    
    }
    
    
    ?>
    What i want to do now however is for the script to add all 3 letter combinations(maybe then this can be changed to 2 letters etc.) behind the word , or after. I basically need to replace the code in $alphabet to something that will do it. Does anyone know what i have to replace this with?

  2. #2
    Join Date
    Dec 2007
    Location
    Lebanon
    Posts
    413
    $alphabet1 = 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");
    $alphabet2 = 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");
    $alphabet3 = 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");

    for($i=0; $i<26; $i++){
    for($j=0; $j<26; $j++){
    for($j=0; $j<26; $j++){
    $threeLetters=$alphabet1[$i].$alphabet2[$j].$alphabet3[$k];
    }
    }
    }


    hope that is what u want

  3. #3
    Join Date
    Dec 2007
    Location
    Lebanon
    Posts
    413
    much better:
    $alphabet= 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");

    for($i=0; $i<26; $i++){
    for($j=0; $j<26; $j++){
    for($j=0; $j<26; $j++){
    $threeLetters=$alphabet[$i].$alphabet[$j].$alphabet[$k];
    }
    }
    }

    sorry for the first

  4. #4
    What exactly is the purpose of this script?

  5. #5
    Join Date
    Dec 2007
    Location
    Lebanon
    Posts
    413
    oups once again hehe replace the last j in the for loop with a k lol

  6. Quote Originally Posted by gregdavidson View Post
    What exactly is the purpose of this script?
    When you are trying to find domains . sometimes you want to add an entire alphabet to the back of the domain to see which are avaiable. and isntead of writing it every time this will help.

    Also , This is what i wrote in my php file :
    Code:
    <form action=<?php echo $_SERVER['PHP_SELF'];?> method=post>
    <input type=text name=input />
    
    <input type=submit name=submit value="Submit" />
    </form>
    
    <?php
    
        
    $alphabet = 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");
    for($i=0; $i<26; $i++){
    for($j=0; $j<26; $j++){
    for($k=0; $k<26; kj++){
    $threeLetters=$alphabet[$i].$alphabet[$j].$alphabet[$k];
    }
    }
    }
    
    $com = ".com";
    
        $word = $_POST[input].$alphabet[$i][$j][$k].$com;
        echo "$word <br>";
    
    }
    
    ?>
    But when i go to it it is just blank . I also tired replacing the

    $word = $_POST[input].$alphabet[$i][$j][$k].$com;
    echo "$word <br>";

    to $threeLetters . But it also didnt work.


    Sorry for being such a noob

  7. #7
    Join Date
    Sep 2004
    Location
    Flint, Michigan
    Posts
    5,765
    This line:

    PHP Code:
    for($k=0$k<26kj++){ 
    should be
    PHP Code:
    for($k=0$k<26$k++){ 
    In addition I think you misunderstood how things were supposed to work. Give this a try:

    PHP Code:
    <form action=<?php echo $_SERVER['PHP_SELF'];?> method=post>
    <input type=text name=input />

    <input type=submit name=submit value="Submit" />
    </form>

    <?php
    // Check to make sure the vairable has been posted.
    if(isset($_POST['input'])) {

        
    // Let's go ahead and put all the letters of the alphabet in an array.    
        
    $alphabet = 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");

        
    // We're now going to enter 3 loops. One for each character position, and loop through all 26 letters.
        
    for($i=0$i<26$i++){
            for(
    $j=0$j<26$j++){
                for(
    $k=0$k<26$k++){
                    echo(
    $_POST['input'].$alphabet[$i].$alphabet[$j].$alphabet[$k].".com");
                echo(
    "<br />");
                }
            }
        }
    // End For Loops
    // End Checking if we're getting a POST

    ?>
    p.s. The PHP tags for BB Code [ PHP ] without the spaces are your friend
    Mike from Zoodia.com
    Professional web design and development services.
    In need of a fresh hosting design? See what premade designs we have in stock!
    Web design tips, tricks, and more at MichaelPruitt.com

  8. #8
    Join Date
    Dec 2007
    Location
    Lebanon
    Posts
    413
    I'm sorry I was somehow asleep when I posted my reply and u didn't repair it well thanks dollar

  9. #9
    Join Date
    May 2007
    Location
    Orange Country, CA
    Posts
    138
    Code:
    for($i=0; $i<26; $i++){
            for($j=0; $j<26; $j++){
                for($k=0; $k<26; $k++){
                    echo($_POST['input'].$alphabet[$i].$alphabet[$j].$alphabet[$k].".com");
                echo("<br />");
                }
            }
        }// End For Loops
    

    Are you kidding?! This is 17,576 iterations!

    If you just want to add 3 random letters to the end of a string just use this:

    Code:
    $random_str = "";
    
    $alphabet = "abcdefghijklmnopqrstuvwxyz";
    
    for ($i=0; $i<3; $i++) {
        $random_str .= $alphabet{rand(0, 25)};
    
    $domain = $_POST['domain'] . $random_str . ".com";
    Blesta - Professional Billing Software
    We are about creating good experiences
    Trial - Demo | 866.478.7567 | Twitter @blesta

  10. #10
    Join Date
    Sep 2004
    Location
    Flint, Michigan
    Posts
    5,765
    That does add three random letters to the back of a string, but the OP wanted to have all possible 3 letter combinations
    Mike from Zoodia.com
    Professional web design and development services.
    In need of a fresh hosting design? See what premade designs we have in stock!
    Web design tips, tricks, and more at MichaelPruitt.com

  11. #11
    Join Date
    May 2007
    Location
    Orange Country, CA
    Posts
    138
    Goodness... I would hope the OP would store all 3 alphabetic combinations in a text file so that they do not EVER have to computed again.
    Blesta - Professional Billing Software
    We are about creating good experiences
    Trial - Demo | 866.478.7567 | Twitter @blesta

  12. #12
    Join Date
    Dec 2007
    Location
    Lebanon
    Posts
    413
    I don't think storing them in a file would be a good idea neither, I think just adding 3 random letters is better

  13. #13
    Join Date
    Sep 2004
    Location
    Flint, Michigan
    Posts
    5,765
    On my local machine it executes in: 0.05981 seconds ( 59.81 ms if you prefer).

    Putting the information in a flat text file and reading line by line executes in: 0.03758 seconds (37.58 ms)

    Using a text file saves you: 22.23 ms ( or about 37%). Certainly an improvement.
    Mike from Zoodia.com
    Professional web design and development services.
    In need of a fresh hosting design? See what premade designs we have in stock!
    Web design tips, tricks, and more at MichaelPruitt.com

  14. #14
    Join Date
    Nov 2005
    Posts
    123
    I get pretty much the opposite results. Reading line by line takes about 30% longer than the generating code (not taking into account the necessary trim(), which adds another c. 50%). But using file_get_contents() and explode() is considerably faster--about 2/3 the time of the generating code.

    By the way, using range('A', 'Z') works as well as array("A", "B", "C", "etc.").

  15. #15
    Join Date
    Sep 2004
    Location
    Flint, Michigan
    Posts
    5,765
    Were you using the code I posted above for the "generate" code? What about the file code?

    PHP Code:
    <form action=<?php echo $_SERVER['PHP_SELF'];?> method=post>
    <input type=text name=input />
    <input type=submit name=submit value="Submit" />
    </form>
    <?php
    if(isset($_POST['input'])) {
        
    $myFile 'chars.txt';
        
    $fp fopen($myFile'r');
        
    $content fread($fpfilesize($myFile));
        
    $arrFp file($myFile);
        foreach(
    $arrFp as $f) {
            echo(
    $_POST['input'] . $f "<br>");
        }
    }
    ?>
    The text file I used had all combinations on a separate line.
    Mike from Zoodia.com
    Professional web design and development services.
    In need of a fresh hosting design? See what premade designs we have in stock!
    Web design tips, tricks, and more at MichaelPruitt.com

  16. #16
    Join Date
    Nov 2005
    Posts
    123
    PHP Code:
    $threeLetters = array();
    $start microtime(true);
    $str trim(file_get_contents('test.txt'));
    $threeLetters explode("\n"$str);
    echo 
    microtime(true) - $start '<br />';
    echo 
    count($threeLetters) . '<br />';
     
    $threeLetters = array();
    $start microtime(true);
    $fp fopen('test.txt''r');
    while (!
    feof($fp)) {
        
    $threeLetters[] = trim(fread($fp4));
    }
    fclose($fp);
    echo 
    microtime(true) - $start '<br />';
    echo 
    count($threeLetters) . '<br />';
     
    $threeLetters = array();
    $start microtime(true);
    $fp fopen('test.txt''r');
    while (!
    feof($fp)) {
        
    $threeLetters[] = trim(fread($fp4));
    }
    fclose($fp);
    echo 
    microtime(true) - $start '<br />';
    echo 
    count($threeLetters) . '<br />'
    ...all combinations on a separate line.
    Yes. Although I doubt that matters much. A separating character is one character, whether a newline, space, comma, or anything else.
    Last edited by bigfan; 02-02-2008 at 10:39 AM.

  17. #17
    Join Date
    Dec 2007
    Location
    Lebanon
    Posts
    413
    it is somehow obvious that reading from a big file with plenty of lines and then working on data so that it becomes usable it will take more time than generating it on the fly

  18. #18
    Join Date
    Nov 2005
    Posts
    123
    Why is that obvious, when two people have said their tests show the opposite? Did you try my tests or dollar's, or even your own?

  19. #19
    Join Date
    Sep 2004
    Location
    Flint, Michigan
    Posts
    5,765
    We must have a bit of difference in our text file. The first example you gave has the right number of records, but the follow two list:

    21971 records

    In any event the best I get is from the third version (execution time wise). I also removed the echoing as you weren't echoing each to the screen which sped things up a decent amount.

    If I take the one I had posted and just have it count it goes down to 14.36 ms.

    If I take the one that generates everything on the fly it's at 20.33ms (without echoing all the text to the screen).

    If I take the third one you gave (adding in the check for a post, the form, removing the built in time checking, etc..) I get: 68.53ms

    This is using Zend Studio to profile the scripts.
    Mike from Zoodia.com
    Professional web design and development services.
    In need of a fresh hosting design? See what premade designs we have in stock!
    Web design tips, tricks, and more at MichaelPruitt.com

  20. #20
    Join Date
    Dec 2007
    Location
    Lebanon
    Posts
    413
    I used your code (bigfan) I got 71.08 ms for reading the file whereas on the fly took 62.22ms

  21. #21
    Join Date
    Nov 2005
    Posts
    123
    codebird-

    Well, all that about echoing stuff and so forth doesn't really matter, as long as all tests produce identical arrays, or output, or whatever you want. The relative times are what matter.

    If you get 21971 for any of the tests, then you have a bug somewhere. The last two do give 17577. Adding something like $tmp = & array_pop($threeLetters) will get rid of the extra (empty) one without significantly affecting the time.

    I accidentally posted the same test twice (second and third). The third test should have been
    PHP Code:
    $threeLetters = array();
    $start microtime(true);
    $fp fopen('test.txt''r');
    while (!
    feof($fp)) {
    $threeLetters[] = trim(fgets($fp));
    }
    fclose($fp);
    $val = & array_pop($threeLetters);
    echo 
    microtime(true) - $start '<br />';
    echo 
    count($threeLetters) . '<br />'
    Here are typical times I get for the three tests, in order:
    0.0209059715271
    0.0900068283081
    0.089478969574

  22. #22
    Join Date
    Dec 2007
    Location
    Lebanon
    Posts
    413
    results on my laptop:

    on the fly: 0.0436449050903
    from a file: 0.0312888622284

    don't know I think it depends where is the script running it depends on the server

  23. #23
    Join Date
    Nov 2005
    Posts
    123
    Codebird-

    This code:
    PHP Code:
    $threeLetters = array();
    $start microtime(true);
    $alphabet range('A''Z');
    for (
    $i 0$i 26$i++){
        for (
    $j 0$j 26$j++){
            for (
    $k 0$k 26$k++){
                
    $threeLetters[] = $alphabet[$i] . $alphabet[$j] . $alphabet[$k];
            }
        }
    }
    echo 
    microtime(true) - $start '<br />';
    echo 
    count($threeLetters) . '<br />'
    comes in at around 45ms. Compare that to the times for my other tests, of course. (Windows XP desktop (2), PHP 5.2.4)

  24. #24
    Join Date
    Dec 2007
    Location
    Lebanon
    Posts
    413
    here are the full results of your script on my laptop (bigfan)

    0.0140569210052
    17576
    0.0502729415894
    17577
    0.0312888622284
    17577

  25. #25
    Join Date
    Dec 2007
    Location
    Lebanon
    Posts
    413
    Quote Originally Posted by bigfan View Post
    Codebird-

    This code:
    PHP Code:
    $threeLetters = array();
    $start microtime(true);
    $alphabet range('A''Z');
    for (
    $i 0$i 26$i++){
        for (
    $j 0$j 26$j++){
            for (
    $k 0$k 26$k++){
                
    $threeLetters[] = $alphabet[$i] . $alphabet[$j] . $alphabet[$k];
            }
        }
    }
    echo 
    microtime(true) - $start '<br />';
    echo 
    count($threeLetters) . '<br />'
    comes in at around 45ms. Compare that to the times for my other tests, of course. (Windows XP desktop (2), PHP 5.2.4)
    result: 0.0359950065613
    17576

  26. #26
    Join Date
    Nov 2005
    Posts
    123
    You can see my favorite machine is slower than yours.

    edit: And at least your laptop vindicates my results.

  27. #27
    Join Date
    Dec 2007
    Location
    Lebanon
    Posts
    413
    oooo i'm working on Linux Ubuntu Gutsy I think all that depends on the computer and the OS so no way to tell which is always better

  28. #28
    Join Date
    Nov 2005
    Posts
    123
    I'll try it on CentOS in a little while.

  29. #29
    Join Date
    Nov 2005
    Posts
    123
    Here are typical numbers I get with my CentOS machine:

    file_get_contents() method:
    0.00598096847534

    on the fly method:
    0.0113561153412

    fgets() method:
    0.0193479061127

    fread() method:
    0.0197949409485

    The numbers on a Gentoo vps and another XP are similar, though slightly higher for all. Each test was run as a separate script, multiple times, with no changes or additions to what I posted here. The times vary negligibly from run to run.

    If slower times were gotten for the file_get_contents() method than for on-the-fly it would probably have to do with slower disk drives.
    Last edited by bigfan; 02-02-2008 at 12:37 PM.

  30. #30
    Join Date
    Dec 2007
    Location
    Lebanon
    Posts
    413
    yes It does vary depending on the hard disk and the computer cause on my desktop windows xp apache2triad it gave somehow completely other resutls

  31. #31
    Join Date
    Feb 2003
    Location
    Canada
    Posts
    958
    The equivalent code in Ruby.

    Code:
    #!/usr/bin/env ruby
    
    ('aaa' .. 'zzz').to_a.each {|s| puts s}
    It profiled at 0.12 seconds.

  32. #32
    Join Date
    Sep 2004
    Location
    Flint, Michigan
    Posts
    5,765
    Quote Originally Posted by Adam-AEC View Post
    The equivalent code in Ruby.

    Code:
    #!/usr/bin/env ruby
    
    ('aaa' .. 'zzz').to_a.each {|s| puts s}
    It profiled at 0.12 seconds.
    Interesting, can you profile one of the other ones for comparison? If you don't mind that is.
    Mike from Zoodia.com
    Professional web design and development services.
    In need of a fresh hosting design? See what premade designs we have in stock!
    Web design tips, tricks, and more at MichaelPruitt.com

  33. #33
    Join Date
    Nov 2005
    Posts
    123
    Here's another one that seems to be a little faster than the other PHP on-the-fly methods:
    PHP Code:
    for ($i 'A'$i != 'AA'$i++) {
        for (
    $j 'A'$j != 'AA'$j++) {
            for (
    $k 'A'$k != 'AA'$k++) {
                
    $threeLetters[] = $i $j $k;
            }
        }

    Adam-AEC-

    Is that one equivalent in that it results in a usable array containing all the combinations?

  34. #34
    Join Date
    Feb 2003
    Location
    Canada
    Posts
    958
    Quote Originally Posted by bigfan View Post
    Here's another one that seems to be a little faster than the other PHP on-the-fly methods:
    PHP Code:
    for ($i 'A'$i != 'AA'$i++) {
        for (
    $j 'A'$j != 'AA'$j++) {
            for (
    $k 'A'$k != 'AA'$k++) {
                
    $threeLetters[] = $i $j $k;
            }
        }

    Adam-AEC-

    Is that one equivalent in that it results in a usable array containing all the combinations?
    Yes, it creates an array. I just walk the array to output it.

    Without output
    Code:
    ('aaa' .. 'zzz').to_a

  35. #35
    Join Date
    Feb 2003
    Location
    Canada
    Posts
    958
    Quote Originally Posted by dollar View Post
    Interesting, can you profile one of the other ones for comparison? If you don't mind that is.
    PHP Script
    Code:
    <?php
      $threeLetters = array();
      $start = microtime(true); 
      $alphabet = range('A', 'Z');
      for ($i = 0; $i < 26; $i++){
        for ($j = 0; $j < 26; $j++){
            for ($k = 0; $k < 26; $k++){
                $threeLetters[] = $alphabet[$i] . $alphabet[$j] . $alphabet[$k];
            }
        }
      }
      echo "Duration: " . ((microtime(true) - $start)) . " microseconds\n";
    ?>
    Results
    Duration: 0.0164301395416 microseconds

    Ruby Script without Output
    Code:
    require 'rubygems'
    require 'ruby-prof'
    
    
    result = RubyProf.profile do
      ('aaa' .. 'zzz').to_a
    end
    
    printer = RubyProf::FlatPrinter.new(result)
    printer.print(STDOUT, 0)
    Results
    Duration: 0.04 seconds

  36. #36
    Join Date
    Nov 2005
    Posts
    123
    How does that compare in time to the PHP?

    nm - I see your post now.

  37. #37
    Join Date
    Nov 2005
    Posts
    123
    Thanks.

    I think you have a typo there, though. "microseconds" should be "seconds".

  38. #38
    Join Date
    Feb 2003
    Location
    Canada
    Posts
    958
    @bigfan
    Yeah, that would make more sense.

  39. #39
    Join Date
    Nov 2005
    Posts
    123
    Quicker yet (still slower than, but approaching, file_get_contents()):
    PHP Code:
    $str 'AAA';
    while (
    $str != 'AAAA') {
        
    $threeLetters[] = $str++;


  40. #40
    Join Date
    Apr 2000
    Location
    California
    Posts
    3,051
    There are a ton of ways to do this in a lot of languages. I doubt that the tiny differences in the speeds will make a huge difference, but the faster/more efficient, the better. Using Perl, I was seeing about 5 times the speed over the ruby example, but that was on the command line (not via CGI or rails testing).

Posting Permissions

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