Php 将大量for循环重写为更短的内容

Php 将大量for循环重写为更短的内容,php,string,for-loop,Php,String,For Loop,我有以下代码: for($a=1; $a<strlen($string); $a++){ for($b=1; $a+$b<strlen($string); $b++){ for($c=1; $a+$b+$c<strlen($string); $c++){ for($d=1; $a+$b+$c+$d<strlen($string); $d++){ $tempString = substr_rep

我有以下代码:

for($a=1; $a<strlen($string); $a++){
    for($b=1; $a+$b<strlen($string); $b++){
        for($c=1; $a+$b+$c<strlen($string); $c++){
            for($d=1; $a+$b+$c+$d<strlen($string); $d++){
                $tempString = substr_replace($string, ".", $a, 0);  
                $tempString = substr_replace($tempString, ".", $a+$b+1, 0); 
                $tempString = substr_replace($tempString, ".", $a+$b+$c+2, 0);
                $tempString = substr_replace($tempString, ".", $a+$b+$c+$d+3, 0);
                echo $tempString."</br>";
            }
        }
    }
}

for($a=1;$a你的问题是一个组合问题。注意:我不是数学怪胎,我只是出于兴趣才研究这些信息

也称为
n选择k
。这是一个函数,用于提供组合数

我在这里找到了一个函数:

要获得所有的组合,还必须在不同的算法之间进行选择

好帖子:

更新:

我发现了一个用不同编程语言编写的算法

我已将其转换为PHP:

function bitprint($u){
    $s= [];
    for($n= 0;$u > 0;++$n, $u>>= 1) {
        if(($u & 1) > 0) $s[] = $n;
    }
    return $s;
}

function bitcount($u){
    for($n= 0;$u > 0;++$n, $u&= ($u - 1));
    return $n;
}

function comb($c, $n){
    $s= [];
    for($u= 0;$u < 1 << $n;$u++) {
        if(bitcount($u) == $c) $s[] = bitprint($u);
    }
    return $s;
}

echo '<pre>';
print_r(comb(4, 6));
函数位打印($u){
$s=[];
对于($n=0;$u>0;+$n,$u>>=1){
如果($u&1)>0)$s[]=n;
}
返回$s;
}
函数位计数($u){
对于($n=0;$u>0;+$n,$u&=($u-1));
返回$n;
}
函数梳($c,$n){
$s=[];

对于($u=0;$u<1而言,这是一个非常不寻常的问题,但我忍不住要总结一下您要做的事情。我的猜测是,您希望看到有多少个字符串组合,其中一个点在字符之间移动,最后在最后一个字符之前停止

我的理解是,您需要一个计数和一个字符串的打印输出,类似于您在这里看到的内容:

class DotCombos
{
    public $combos;

    private function combos($string)
    {
        $rebuilt = "";
        $characters = str_split($string);
        foreach($characters as $index => $char) {
            if($index == 0 || $index == count($characters)) {
                continue;
            } else if(isset($characters[$index]) && $characters[$index] == ".") {
                break;
            } else {
                $rebuilt = substr($string, 0, $index) . "." . substr($string, $index);
                print("$rebuilt\n");
                $this->combos++;
            }
        }
        return $rebuilt;
    }

    public function allCombos($string)
    {
        if(strlen($string) < 2) {
            return null;
        }
        $this->combos = 0;
        for($i = 0; $i < count(str_split($string)) - 1; $i++) {
            $string = $this->combos($string);
        }
    }
}
为了实现此功能,我提出了一个类,通过这种方式,您可以将其移植到代码的其他部分,并且它可以处理多个字符串。这里的警告是字符串必须至少包含两个字符,并且不包含句点。下面是该类的代码:

$combos = new DotCombos();
$combos->allCombos("test123");
print("Count: $combos->combos");
产出将是:


希望这就是你想要的(或者至少是帮助)..

最终结果是什么?所有字母之间的点?@Crackertastic,你是对的,请参见上面的示例。我上面展示的这些循环使用4个点进行所有可能的组合。如果我的理解是正确的,那么你需要做的就是使用一个函数在字符串中移动周期(随时间计数)同时还要确保它在字符串中的另一个句点之前停止。第二个函数负责根据字符串大小进行循环。我已经发布了一个答案供您考虑,说明了我的意思。好吧,我的问题比我想的更复杂。我仍然不知道如何做::/但是感谢这些链接。哇,真的几乎是完美的,我只是无法在一次调用函数comb中获得所有组合。让我检查一下是否可以多次调用它并获得所有可能的组合。我已使其正常工作。:)我希望您添加此项以使其完整:$combs=[];for($i=0;$inice..我已经将它添加到了答案中感谢您添加了它并提供了解决方案!;)当然它会有帮助!但是还有一些其他的组合,该类不会生成,比如t.e.st12.3Ah,我想我现在看到了您所追求的更好一些。好吧,这应该给您一个开始。我将留给您来完成它:)
t.est
te.st
tes.t
t.es.t
te.s.t
t.e.s.t
count: 6
class DotCombos
{
    public $combos;

    private function combos($string)
    {
        $rebuilt = "";
        $characters = str_split($string);
        foreach($characters as $index => $char) {
            if($index == 0 || $index == count($characters)) {
                continue;
            } else if(isset($characters[$index]) && $characters[$index] == ".") {
                break;
            } else {
                $rebuilt = substr($string, 0, $index) . "." . substr($string, $index);
                print("$rebuilt\n");
                $this->combos++;
            }
        }
        return $rebuilt;
    }

    public function allCombos($string)
    {
        if(strlen($string) < 2) {
            return null;
        }
        $this->combos = 0;
        for($i = 0; $i < count(str_split($string)) - 1; $i++) {
            $string = $this->combos($string);
        }
    }
}
$combos = new DotCombos();
$combos->allCombos("test123");
print("Count: $combos->combos");
t.est123
te.st123
tes.t123
test.123
test1.23
test12.3
t.est12.3
te.st12.3
tes.t12.3
test.12.3
test1.2.3
t.est1.2.3
te.st1.2.3
tes.t1.2.3
test.1.2.3
t.est.1.2.3
te.st.1.2.3
tes.t.1.2.3
t.es.t.1.2.3
te.s.t.1.2.3
t.e.s.t.1.2.3
Count: 21