PHP 制作 4char AZ,az,0-9 的所有可能变体

发布于 2024-10-13 00:54:49 字数 185 浏览 3 评论 0原文

我必须列出 4 个字符 AZ、az、0-9 的所有可能排列以及所有这些的组合。我如何才能通过所有可能的组合并打印它们?

它的用途:我需要在一个 html 文档中制作它,然后我可以打印该文档并将所有这些作为我们大学的随机唯一用户名提供,以便学生可以根据一个唯一的 ID 提供反馈,该 ID 在使用时将失效。我无法将这个程序更改为更好的程序!

I have to make a list of all possible permurations of 4characters A-Z,a-z,0-9 and conbination of all this.How can i pass thru all of the possible combinations and printf them ?

what's it for:I need to make this in a html document that i can then print and give all this as random unique usernames for our university, so that students can provide feedback based on one unique id that will be invalidated when used. i can not change this procedure into a better one!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

芸娘子的小脾气 2024-10-20 00:54:49

警告:这需要一些时间来计算,因为有 62^4 = 14776336 种可能的组合。如果您累积结果并且不直接打印它们,也会占用大量内存。

function print_combinations($characters, $length, $combination = '') {
        if ($length > 0) {
            foreach ($characters as $i) {
                print_combinations($characters, $length - 1, $combination . $i);
            }
        } else {
            printf("%s\n", $combination);
        }
}

$characters = array_merge(range('A', 'Z'), range('a', 'z'), range(0, 9));
print_combinations($characters, 4);

Warning: This takes some time to compute because there are 62^4 = 14776336 possible combinations. It also takes a lot of memory if you accumulate the results and don't print them directly.

function print_combinations($characters, $length, $combination = '') {
        if ($length > 0) {
            foreach ($characters as $i) {
                print_combinations($characters, $length - 1, $combination . $i);
            }
        } else {
            printf("%s\n", $combination);
        }
}

$characters = array_merge(range('A', 'Z'), range('a', 'z'), range(0, 9));
print_combinations($characters, 4);
青柠芒果 2024-10-20 00:54:49

通过一种非常规的方法,您可以使用 评论中的 dec2any base_convert 的 php 文档如下所示:

$index = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$base = strlen($index);
$len = 4;

for ($i = 0, $l = pow(strlen($index), $len); $i < $l; $i++) {
    echo str_pad(dec2any($i, $base, $index), $len, "0", STR_PAD_LEFT), "\n";
} 

function dec2any( $num, $base=62, $index=false ) {
    if (! $base ) {
        $base = strlen( $index );
    } else if (! $index ) {
        $index = substr( "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ,0 ,$base );
    }
    $out = "";
    for ( $t = floor( log10( $num ) / log10( $base ) ); $t >= 0; $t-- ) {
        $a = floor( $num / pow( $base, $t ) );
        $out = $out . substr( $index, $a, 1 );
        $num = $num - ( $a * pow( $base, $t ) );
    }
    return $out;
}

通过更改 $index 和 $len 可以很容易地对其进行调整。

With a rather unconventional approach, you could use dec2any from the comments on the php documentation for base_convert like this:

$index = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$base = strlen($index);
$len = 4;

for ($i = 0, $l = pow(strlen($index), $len); $i < $l; $i++) {
    echo str_pad(dec2any($i, $base, $index), $len, "0", STR_PAD_LEFT), "\n";
} 

function dec2any( $num, $base=62, $index=false ) {
    if (! $base ) {
        $base = strlen( $index );
    } else if (! $index ) {
        $index = substr( "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ,0 ,$base );
    }
    $out = "";
    for ( $t = floor( log10( $num ) / log10( $base ) ); $t >= 0; $t-- ) {
        $a = floor( $num / pow( $base, $t ) );
        $out = $out . substr( $index, $a, 1 );
        $num = $num - ( $a * pow( $base, $t ) );
    }
    return $out;
}

It can be quite easily adapted by changing $index and $len.

慕巷 2024-10-20 00:54:49

虽然有点仓促,但是:

class Combinations{

    protected $characters = array();
    protected $combinations = array();

    public function __construct($characters){
        $this->characters = $characters;
    }

    public function getCombinations($length){       

        foreach($this->characters as $comb)
        {
            $this->getRecurse($comb, $length - 1);
        }

    $combinations = $this->combinations;
    $this->combinations = array();

    return $combinations;

    }

    public function getRecurse($combination, $length){

        if($length <= 0){
            $this->combinations[] = $combination;
        }else{
            foreach($this->characters as $comb)
            {
                $this->getRecurse($combination.$comb, $length - 1);
            }
        }

    }

}

$characters = array_merge(range('A', 'Z'), range('a', 'z'), range(0, 9));

$comb = new Combinations($characters);

print_r( $comb->getCombinations(4) );

传递给 getCombinations 的数字是您希望组合的长度。

It's a little rushed but:

class Combinations{

    protected $characters = array();
    protected $combinations = array();

    public function __construct($characters){
        $this->characters = $characters;
    }

    public function getCombinations($length){       

        foreach($this->characters as $comb)
        {
            $this->getRecurse($comb, $length - 1);
        }

    $combinations = $this->combinations;
    $this->combinations = array();

    return $combinations;

    }

    public function getRecurse($combination, $length){

        if($length <= 0){
            $this->combinations[] = $combination;
        }else{
            foreach($this->characters as $comb)
            {
                $this->getRecurse($combination.$comb, $length - 1);
            }
        }

    }

}

$characters = array_merge(range('A', 'Z'), range('a', 'z'), range(0, 9));

$comb = new Combinations($characters);

print_r( $comb->getCombinations(4) );

The number passed to getCombinations is how long you want the combinations to be.

我喜欢麦丽素 2024-10-20 00:54:49

使用分隔符的更通用方法:

function getCombinations($base,$delimiter="|"){

    $base = array_values($base);
    $baselen = count($base);

    if($baselen == 0){
        return;
    }
    if($baselen == 1){
        return $base[0];
    }else{
        //get one level lower combinations
        $oneLevelLowerArray = $base;
        $orig_part = $oneLevelLowerArray[0];
        unset($oneLevelLowerArray[0]);
        $oneLevelLower = getCombinations($oneLevelLowerArray,$delimiter);
        $countOLL = count($oneLevelLower);
        foreach ($orig_part as $rowa) {
            foreach ($oneLevelLower as $rowb) {
                $resultArray[] = $rowa.$delimiter.$rowb;
            }
        }

    }

    return $resultArray;


}

More universal approach with delimiter :

function getCombinations($base,$delimiter="|"){

    $base = array_values($base);
    $baselen = count($base);

    if($baselen == 0){
        return;
    }
    if($baselen == 1){
        return $base[0];
    }else{
        //get one level lower combinations
        $oneLevelLowerArray = $base;
        $orig_part = $oneLevelLowerArray[0];
        unset($oneLevelLowerArray[0]);
        $oneLevelLower = getCombinations($oneLevelLowerArray,$delimiter);
        $countOLL = count($oneLevelLower);
        foreach ($orig_part as $rowa) {
            foreach ($oneLevelLower as $rowb) {
                $resultArray[] = $rowa.$delimiter.$rowb;
            }
        }

    }

    return $resultArray;


}
那些过往 2024-10-20 00:54:49

类似的事情? (伪代码)

$a = array_merge(range('A', 'Z'), range('a', 'z'), range(0, 9));

foreach($a as $key => $b) {
    echo $b.$a[$key+1].$a[$key+2].$a[$key+3].'<br />';
}

Something like that? (pseudo code)

$a = array_merge(range('A', 'Z'), range('a', 'z'), range(0, 9));

foreach($a as $key => $b) {
    echo $b.$a[$key+1].$a[$key+2].$a[$key+3].'<br />';
}
星星的轨迹 2024-10-20 00:54:49
$numbers = range(0, 9);
$lowerCaseLetters = range('a', 'z');
$upperCaseLetters = range('A', 'Z');

foreach($numbers as $number) {
   foreach($lowerCaseLetters as $lowerCaseLetter) {
      foreach($uperCaseLetters as $upperCaseLetter) {
          echo $number.$lowerCaseLetter.$upperCaseLetter.'<br />';
          echo $number.$upperCaseLetter.$lowerCaseLetter.'<br />';
          echo $lowerCaseLetter.$number.$upperCaseLetter.'<br />';
          echo $lowerCaseLetter.$upperCaseLetter.$number.'<br />';
          echo $upperCaseLetter.$lowerCaseLetter.$number.'<br />';
          echo $upperCaseLetter.$number.$lowerCaseLetter.'<br />';
      }
   }
}
$numbers = range(0, 9);
$lowerCaseLetters = range('a', 'z');
$upperCaseLetters = range('A', 'Z');

foreach($numbers as $number) {
   foreach($lowerCaseLetters as $lowerCaseLetter) {
      foreach($uperCaseLetters as $upperCaseLetter) {
          echo $number.$lowerCaseLetter.$upperCaseLetter.'<br />';
          echo $number.$upperCaseLetter.$lowerCaseLetter.'<br />';
          echo $lowerCaseLetter.$number.$upperCaseLetter.'<br />';
          echo $lowerCaseLetter.$upperCaseLetter.$number.'<br />';
          echo $upperCaseLetter.$lowerCaseLetter.$number.'<br />';
          echo $upperCaseLetter.$number.$lowerCaseLetter.'<br />';
      }
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文