Created
March 12, 2015 16:17
-
-
Save apisurfer/1e515cc029da0e5b4c41 to your computer and use it in GitHub Desktop.
Simple hack based on previous script that allows setting the starting string. It's also quite likely not the ideal and fastest solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Set max string length and charset for generating combinations | |
$maxLength = 3; | |
$charSet = 'abcdph'; | |
$size = strlen($charSet); | |
function initBase ($init, $set) { | |
$initBase = str_split(strrev($init)); | |
$positions = array(); | |
foreach ($initBase as $letter) { | |
if ( strpos($set, $letter ) === false) { | |
echo "No character '$letter' in given charSet! Please update the charset.<br>"; | |
return false; | |
} else { | |
$positions[] = strpos($set, $letter); | |
} | |
} | |
return $positions; | |
} | |
$counter = 0; | |
$base = initBase('php', $charSet); | |
$baseSize = count($base); | |
$firstRun = true; | |
while($baseSize <= $maxLength) { | |
// Go through all the possible combinations of last character and output $base | |
if ($firstRun) $i = $base[0]; | |
else $i = 0; | |
for(; $i < $size; $i++) { | |
$base[0] = $i; | |
for($j = $baseSize - 1; $j >= 0; $j--) { | |
echo $charSet[$base[$j]]; | |
} | |
echo '<br/>'; | |
} | |
$firstRun = false; | |
// How many $base elements reached their max? | |
for($i = 0; $i < $baseSize; $i++) { | |
if($base[$i] == $size-1) $counter++; | |
else break; | |
} | |
// Every array element reached max value? Expand array and set values to 0. | |
if($counter == $baseSize) { | |
// Notice <=$baseSize! Initialize 0 values to all existing array elements and ADD 1 more element with that value | |
for($i = 0; $i <= $baseSize; $i++) { | |
$base[$i] = 0; | |
} | |
$baseSize = count($base); | |
} | |
// Carry one | |
else { | |
$base[$counter]++; | |
for($i = 0; $i < $counter; $i++) $base[$i] = 0; | |
} | |
$counter=0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment