1

I have user account rule is limit 20 symbols can be letters, period, underscore and numbers.
q1. How to print all possible string? below is my code I can't find the way mix different symbols
q2. Is this the correct way print website sitemap because it is 39 nonillion strings I found https://stackoverflow.com/a/1099421 but I need to know more detail understand usually people how to do it?

$letters = 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');
$period = array('.');
$underscore = array('_');
$numbers = array('0','1','2','3','4','5','6','7','8','9');

for ($i=0; $i < count($letters); $i++) { 
  for ($ii=0; $ii < count($period); $ii++) { 
    for ($iii=0; $iii < count($underscore); $iii++) { 
      for ($iiii=0; $iiii < count($numbers); $iiii++) { 
        // echo $numbers[$iiii]+$letters[$i]+ ...
      }
    }
  }
}

edit:
base on below answer I try to make a xml siteamp but it loops only one symbol length?

$domDocument = new DOMDocument('1.0', 'UTF-8');

$domElementUrlSet = $domDocument->createElement('urlset'); 
$domElementUrlSet->appendChild(
  new DomAttr('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9')
);

$domDocument->appendChild($domElementUrlSet);



$chars = 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','.','_','0','1','2','3',
    '4','5','6','7','8','9'
);
$length = 2;
$charsLength = count($chars);
$current = array_fill(0, $length, -1);
$end = array_fill(0, $length, $charsLength - 1);

while ($current != $end) {
    // increment current state
    $n = $length;
    while ($n-- >= 0) {
        $current[$n]++;
        if ($current[$n] == $charsLength) {
            $current[$n] = 0;
        } else {
            break;
        }
    }
    // print string
    for ($i=0; $i < $length; $i++) { 
      if ($current[$i] >= 0) {
        // echo $chars[$current[$i]];

        $url = $domDocument->createElement('url'); 
        $url->appendChild( $loc = $domDocument->createElement('loc', 'http://www.example.com/'.$chars[$current[$i]]) ); 

        $domElementUrlSet->appendChild($url);
      }
    }
    // echo PHP_EOL;
}

echo $domDocument->saveXML();
Community
  • 1
  • 1
vibskov
  • 275
  • 4
  • 16
  • I would put all the letters into *one* array. Then check this: http://docstore.mik.ua/orelly/webprog/pcook/ch04_26.htm .. However, in your case it will be **many** permutations. – hek2mgl Apr 12 '15 at 08:50
  • 3
    That will be 38^20 = 39 Nonillion strings. Not possible. – Jimmy T. Apr 12 '15 at 10:00
  • To continue what @JimmyT. says, *why* do you need to print *all* the combinations? – Abhay Apr 12 '15 at 10:56
  • @Abhay because I have a website I need to print all username make sitemap . if it is not possible, wondering people how to do this such like stackoverflow instagram have large data and if the each page need crawler index – vibskov Apr 12 '15 at 17:10
  • All _possible_ usernames is _very_ different and _much much_ larger from all _existing_ ones. What you want is the latter, which of course comes from the user database. – Abhay Apr 12 '15 at 23:06
  • @Abhay , JimmyT. thanks for reply! what kind of result will happen if I execute the function like below answer? and what is the possible number can be run? (I check http://data.stackexchange.com/ stackoverflow have 9.2m ) – vibskov Apr 13 '15 at 01:09
  • It is already answered by @JimmyT. Probably the lifespan of this universe is not enough for going through all the combinations. – Abhay Apr 13 '15 at 01:51
  • @Abhay wow! thanks. because I don't have access to database get all exist users. – vibskov Apr 13 '15 at 02:42
  • Then there is no way out. – Abhay Apr 13 '15 at 10:40
  • @vibskov What are you trying to achieve with a sitemap with virtually no working links? – Jimmy T. Apr 13 '15 at 19:38
  • @JimmyT. I try to help my client index their website all user account in sitemap, but I can't get access to user database, they only give me their user registration rule... – vibskov Apr 28 '15 at 12:51
  • That sitemap would be useless. – Jimmy T. Apr 28 '15 at 19:41
  • @JimmyT. why? and then how to make google index every user page, my url like this `example.com/user/:userid` , can I make wildcard in sitemap like `example.com/user/*` ? – vibskov Apr 29 '15 at 00:30
  • 1
    First, google will probably not go through all links if almost all of them don't work. Seconly, even if google would scan the whole file it couldn't finish because the earth won't exist till then. Wildcard sitemaps don't work because HTTP doesn't allow to list files of a directory. The only thing you can do is to get the list of the actual users. – Jimmy T. Apr 30 '15 at 05:58
  • @JimmyT. thanks for description – vibskov Apr 30 '15 at 14:43

1 Answers1

0

Task is quite trivial. You don't need separate sets chars, just single alphabet and iterate through all possible combinations:

<?php
$chars = 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','.','_','0','1','2','3',
    '4','5','6','7','8','9'
);
$length = 20;
$charsLength = count($chars);
$current = array_fill(0, $length, -1);
$end = array_fill(0, $length, $charsLength - 1);

while ($current != $end) {
    // increment current state
    $n = $length;
    while ($n-- >= 0) {
        $current[$n]++;
        if ($current[$n] == $charsLength) {
            $current[$n] = 0;
        } else {
            break;
        }
    }
    // print string
    for ($i=0; $i < $length; $i++) { 
        if ($current[$i] >= 0) echo $chars[$current[$i]];
    }
    echo PHP_EOL;
}
  • Thanks!! this correct to print the string. but I execute this , lenght is set 20 there is error `the maximum execution time of 30 seconds`, I found http://stackoverflow.com/a/15904047/1927742 . but as above @Jimmy T. 's comment should I doing this? – vibskov Apr 12 '15 at 17:32
  • well, this operation takes time, and you should use `ini_set('max_execution_time', 0);` to prevent timeout termination. – Andrii Kucherenko Apr 12 '15 at 18:48
  • 1
    Would it help the OP to specify the approximate total number of combinations expected and how long it may take to complete at, say, one million combinations per second? Just so they know how long to wait for the answers? – Ryan Vincent Apr 12 '15 at 21:18
  • @zless please see my edit I use your code try create a xml file, I can't find why loop in only one length symbol? – vibskov Apr 13 '15 at 00:45