function create_word ($alphabet, $index) {
....
return $word;
}
create_word ("abc", 9); // "bc"
create_word ("abc", 18); // "abc"
function create_word(string $str, int $cnt): string
{
$len = \strlen($str);
if ($len === 0 || $cnt === 0) {
return '';
}
$result = '';
while ($cnt > $len) {
$i = ($cnt % ($len)) ?: $len;
$result = $str[$i - 1] . $result;
$cnt = ($cnt - $i) / $len;
}
$result = $str[$cnt - 1] . $result;
return $result;
}
echo create_word('abc', 9); //'bc'
echo create_word('abc', 18); //'abc'
$text_lenght = 3;
$charset = 'abc';
$charset_length = strlen($charset);
function recurse($width, $position, $base_string)
{
global $text_lenght, $charset, $charset_length;
for ($i = 0; $i < $charset_length; ++$i) {
if ($position < $width - 1) {
recurse($width, $position + 1, $base_string . $charset[$i]);
}
if (strlen($base_string . $charset[$i]) <= $text_lenght) {
echo $base_string . $charset[$i].'</br>';
}
}
}
recurse($text_lenght, 0, '');
function create_word($aplhabet, $index) {
$words = [];
$prevLevelWords = [""];
foreach (range(1, strlen($aplhabet)) as $offset) {
$currentLevelWords = [];
foreach (str_split($aplhabet) as $letter) {
foreach ($prevLevelWords as $prevLevelWord) {
$word = $letter . $prevLevelWord;
$currentLevelWords[] = $word;
$words[] = $word;
}
}
$prevLevelWords = $currentLevelWords;
}
return $words[$index-1] ?? false;
}
function create_word($alphabet, $index) {
$index = $index - 1;
if ($index < strlen($alphabet)) {
return str_split($alphabet)[$index];
}
return create_word($alphabet, floor($index / strlen($alphabet))) .
create_word($alphabet, $index % strlen($alphabet) + 1);
}