0

如何列出所有可能的组合:

字符串(4 个字符,只有小写字母,没有数字或特殊符号)、# 符号、字符串(5 个字符,与第 1 个相同)。

例如:

dhgi#msodd

4

2 回答 2

6

Assumption: English alphabet

for($firstPart = 'aaaa'; $firstPart != 'aaaaa'; $firstPart++) {
   for($secondPart = 'aaaaa'; $secondPart != 'aaaaaa'; $secondPart++) {
      echo $firstPart,'#',$secondPart,'<br />';
   }
}

Though why you'd want to do this, I don't know.

Is this related to your previous question?

于 2011-02-03T14:37:05.227 回答
0

由递归驱动

  function bruteforce($data)
{
    $storage = array();
    tryCombination($data,'',$storage);
    return $storage;
}


function tryCombination($input,$output,&$storage)
{
    if($input == "")
    {

        if(!in_array($output,$storage))
        array_push($storage,$output);

    }else {
        for($i = 0 ; $i < strlen($input) ; $i++)
        {
            $next = $output  . $input[$i];
            $remaining = substr($input,0,$i)  . substr($input,$i + 1);
            tryCombination($remaining,$next,$storage);
        }
    }


}

$final = bruteforce('yourData');
echo count($final);
print_r($final);
于 2011-02-03T15:45:21.353 回答