PHP查找所有组合

时间:2010-08-13 23:11:47

标签: php string replace

我正在尝试查找单词的所有可能组合,并替换某些字母。

所以,我有以下代码:

<form name="search" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="searchterm" />
<input type="submit" value="Submit"/>
</form>

<?php


function pset($array) {
    $results = array(array());

    foreach ($array as $element)
        foreach ($results as $combination)
            array_push($results, array_merge(array($element), $combination));

    return $results;

}


$searchterm = $_POST["searchterm"];

$search  = array(
                    array("t","7"),
                    array("e","3")
                );



$searchpowerset=pset($search);                 

foreach($searchpowerset as $a)
{
    $newterm = str_replace($a[0][0],$a[0][1],$searchterm);
    echo $newterm . "<br/>";
}


?>

表单中输入的内容为:peter

我希望输出包括:

p3t3r
p373r

目前正在回归:

peter
pe7er
p3t3r
p3t3r

重复不是问题,因为我可以轻松地摆脱这些重复,但我需要能够在每个循环中使用所有替换。

提前致谢。

2 个答案:

答案 0 :(得分:1)

您的代码会替换字符串中每个字符的实例,因此无法正常工作。

我的实施:

$searchterm = $_POST['searchterm'];

$search = array( array('e', '3'), array('t', '7'), array('e', 'E') );

function replace($string, $prefix = '')
{
    global $search;

    // If $string is empty, we've reached the last character,
    // so there is only one permutation. In that case, return it.
    if(!$string) return array($prefix);

    // Otherwise, take the first character in a string,
    // then prepend it to all found combinations of the rest.
    $result = array();
    $letter = substr($string, 0, 1); // <- first character (head of $string)
    $rest   = substr($string,1);     // <- next characters (tail of $string)

    // Find all combinations of $rest (with $prefix and $letter
    // prepended to them), then add them to the $result array.
    $result = array_merge($result, replace($rest, $prefix . $letter));
    foreach($search as $term) {
        // In case the current $letter can be substituted by something in
        // $search, repeat the last step with the replaced character
        // instead of $letter.
        if($term[0] == $letter) {
            $result = array_merge($result, replace($rest, $prefix . $term[1]));
        }
    }

    return $result;
}

var_dump(replace($searchterm));

由于函数必须返回所有组合,因此它会递归调用自身的原始字符和每个替换的字符。

现在,它为什么会起作用?对于每个调用,一个字符从$ string移动到$ prefix。此字符是$ string的第一个字符,或者它是替换值。如果存在替换值,则返回原始结果和修改结果的结果。当没有更多要移动的字符时,它返回$前缀,现在包含整个字符串。

因此,对于字符串'pet',调用树看起来像这样:

[$string, $prefix]
                                     ->['', 'pe7']
                                     |
['pet', '']->['et', 'p']->['t', 'pe']->['', 'pet']
                        |
                        ->['t', 'p3']->['', 'p3t']
                                     |
                                     ->['', 'p37']

从那以后,你可以清楚地看到组合:'pe7','pet','p3t','p37'(尽管顺序不同,但这并不重要)。

答案 1 :(得分:0)

我看到您的代码基于此处显示的O'Reilly PHP Cookbook:http://docstore.mik.ua/orelly/webprog/pcook/ch04_25.htm

我希望列出所有可能的字符组合,这些字符组合在一维数组中作为按字母顺序排列的字符串。我们将O'Reilly代码更改为以下运行良好的代码。

 $lettersArray = array('C', 'A', 'T', 'D', 'O', 'G', 'S');

//Create a tiered results array of all possible combinations of letters
 $results = array(array());
 foreach ($lettersArray as $element) {
     foreach ($results as $combination) {
         array_push($results, array_merge(array($element), $combination));
     }
 }

 //Build combinations array by compacting results array and sorting the letters in to alphabetical order
 $combinations = array();
 foreach ($results as $result){
     sort($result);
     $string = implode($result);
     array_push($combinations, $string);
 }
 sort($combinations);

组合数组现在包含所有可能的字母组合的按字母顺序排列的有序列表。

对于我们的应用程序,我们有一个单词数据库表,其中还包含一个单词,其中包含字母按字母顺序排列的单词。我们将这个输出与我们的拼字游戏数据库相匹配,以制作一个漂亮的拼字游戏单词构建器。

有关输出的示例,请参阅:http://scrabblehints.com

相关问题