Php 在所有可能的字符之间生成点

Php 在所有可能的字符之间生成点,php,algorithm,Php,Algorithm,我正在寻找一个在PHP算法,使输出的所有可能性与点。我们可以把do放在单词的任何地方,但现在允许在每个单词后面重复两个点。例如,“注意”输出如下: note n.ote n.o.te n.o.t.e no.t.e not.e n.ot.e .... 下面的输出也是错误的: n..ote (repeat dots right after each other) .note (put dots at first of word) note. (put dots at end of word) 递

我正在寻找一个在PHP算法,使输出的所有可能性与点。我们可以把do放在单词的任何地方,但现在允许在每个单词后面重复两个点。例如,“注意”输出如下:

note
n.ote
n.o.te
n.o.t.e
no.t.e
not.e
n.ot.e
....
下面的输出也是错误的:

n..ote (repeat dots right after each other)
.note (put dots at first of word)
note. (put dots at end of word)
递归方式:

Put current char of source in the result string
if current char is the last one
     output result
else
     call recursive function with the next char index
     add dot to result and call recursive function with the next char index
迭代方式:

有带点的
2^(Len-1)
组合,其中Len i字长。
k=0..2^(Len-1)-1创建一个循环,并为每个k在这些位置插入点,其中k的二进制表示包含
1
s(k=2=binary 010=>
po.le

通过以下有益的指导,我最终找到了解决方案:

函数stringInsert($str,$insertstr,$pos){
$str=substr($str,0,$pos)。$insertstr.substr($str,$pos);
返回$str;
}
函数生成($var=“note”,$i=0){
$length=strlen($var);
而($i+1<$length){
$i++;
$new=stringInsert($var,'.',$i);
echo$new;
生成($new,$i+1);
}
}
生成(“shaghayegh”);
例如,关键字“note”生成了7个字符串


对于关键字“Shaghayeh”,生成了511个字符串

每个名称有最大点数吗?没有,我需要所有的可能性作为输出,但必须验证两个点没有放置在彼此后面,因此最大点数与字符串的长度相同-1。这是一条线索为此,您可能需要排列和组合。
function stringInsert($str,$insertstr,$pos){
    $str = substr($str, 0, $pos) . $insertstr . substr($str, $pos);
    return $str;
}

function generate($var="note",$i=0){
    $length = strlen($var);

    while ($i+1 < $length) {
        $i++;
        $new = stringInsert($var,'.',$i);
        echo $new;
        generate($new,$i+1);

    }
}


generate('shaghayegh');