Increment through A-Za-z0-9 in expanding string with PHP
<p>I'm trying to generate strings in PHP with a group of valid characters, cycling through them and appending an extra character on the end of the string, until maximum length is reached. For example, desired output:</p>

<p>a,b,c,d,e,f,aa,ab,ac,ad,ae,af,ba,bb,bc,bd,be,bf,ca,cb..etc</p>

<p>This is my PHP function so far:</p>

<pre><code>&lt;?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',
        'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','X','Y','Z',
        '1','2','3','4','5','6','7','8','9','0');
$maxlen = 10;

$input = $chars[0];
while (1):
    echo buildInput($maxlen, $chars, $input) . "\n";
endwhile;

function buildInput($maxlen, $chars, $previous)
{
        if (array_search(substr($previous, -1), $chars) == sizeof($chars) - 1):
                // end of input cycle reached, add another character
                $previous = $previous . $chars[0];
        endif;

        if (strlen($previous) &gt; $maxlen):
                die('Max length reached');
        endif;

        // Remove last character, and append incremented char
        $input = substr($previous, 0, -1);
        $input = $input . $chars[array_search(substr($previous, -1), $chars)+1];

        return $input;
}
?&gt;
</code></pre>

<p>It only increments the last character of the string which gets to 0, then appends 'a' and starts over but without trying all the other possible permutations.</p>

<p>Could someone help me with a better method?</p>
