PHP将字母列表与单词数组进行比较

时间:2011-12-06 21:42:07

标签: php

我正在尝试这样做,以便我可以从表单中导入一个字母列表$rack,并将其与数组$words进行比较,该数组是从文本文件导入的字典。然后我想构建一个数组$playable,它将存储包含$rack中任何字母的所有单词。

这就是我所拥有的

<?php

session_start();
$rack = $_GET["letters"]; // list of letters imported from form

$words = array();  // imported text file with the list of playable words

$playable = array(); // array created by finding playable words from dictionary


//populates $words with the text file dictionary.txt
$filename = 'dictionary2.txt';

$fp = @fopen($filename, 'r'); 

$words = fread($fp); 


// array to score word

$score = array("a" => 1, "b" => 1, "c" => 1, "d" => 1, "e" => 1, "f" => 1, "g" => 1, "h" => 1, 
"i" => 1, "j" => 1, "k" => 1, "l" => 1, "m" => 1, "n" => 1, "o" => 1, "p" => 1, "q" => 1, 
"r" => 1, "s" => 1, "t" => 1, "u" => 1, "v" => 1, "w" => 1, "x" => 1, "y" => 1, "z" => 1); 

// loop that checks each index, or words, against the list of letters the player
// informs us he has while building the second array.

for ( $i=0; $i<sizeof($words); $i++){  
    if (in_array($rack, $words[$i])){
        $playable[] = $words[$i];
        }
}


?>
<html>

<title>Hanging With Friends Help</title>

<body style="background-color:black;">
<font color = "white">
<center><img src="wordslogo.jpg" alt="Cheater!" /> </center>
<form name="input" action="http://i211.soic.indiana.edu/~kmnacke/hangingwithfriends.php" method="get">


    <p>
    <br />
    <br />
    <br />
    <center><input type="text" name="letters" /></center>

    </p>
    <center><input type="submit" value="Submit" /></center>

    <center><label>

    <?php 


 //prints the list of playable words

    for ( $i=0; $i<sizeof($playable); $i++){  
        echo $playable[$i];

}

    for ( $i=0; $i<sizeof($words); $i++){  
            echo $words[$i];

    }


    ?>


    </label></center>


    </form>

</body>

</html>

2 个答案:

答案 0 :(得分:1)

永远不要使用in_array,它应该从PHP中移除......非常非常慢。将array_flipisset结合使用,速度提高了几个数量级。

以下是您想要的代码,它还会根据您为每个字母分配的分数来累计每个单词的分数。

$words = 'these are the words';
$words = explode(' ',$words);

$rack = 'a b c d e f g';
$rack = explode(' ',$rack);
$rack = array_flip($rack);

$score = array("a" => 1, "b" => 1, "c" => 1, "d" => 1, "e" => 1, "f" => 1, "g" => 1, "h" => 1, 
"i" => 1, "j" => 1, "k" => 1, "l" => 1, "m" => 1, "n" => 1, "o" => 1, "p" => 1, "q" => 1, 
"r" => 1, "s" => 1, "t" => 1, "u" => 1, "v" => 1, "w" => 1, "x" => 1, "y" => 1, "z" => 1); 

$playable = array();
$playable_score = array();

foreach($words as $val)
{
$len = strlen($val);
$thisscore = 0;
for($run=0; $run<$len; $run++)
    {
    if (isset($rack[$val[$run]]]) && isset($score[$val[$run]]))
    $thisscore+=$score[$val[$run]];
    }
if ($thisscore>0)
    {
    $playable[]=$val;
    $playable_score[]=$thisscore;
    }
}

另请注意,您可以摆脱$rack并使用$score,假设您一直在计算所有字母。在这种情况下,您可以只为某些字母赋值0.要进行此更改,只需将此行if (isset($rack[$val[$run]]) && isset($score[$rack[$val[$run]]))修改为此if (isset($score[$rack[$val[$run]]))并删除$rack的初始化。

答案 1 :(得分:0)

不知道你真正想要的是哪一个 - 我将如何做到这两点:

有人有更快的方法吗? (我来这里学习!)

这是一个键盘手柄链接:http://codepad.org/nTE8QWkf

<?php
$str = '
what
are
you
doing';

$dict_arr = explode("\n", $str);
$rack_arr = str_split('what')


?>
<pre>
<?php print_r(findwordswithany($dict_arr, $rack_arr)); ?>
</pre>
<pre>
<?php print_r(findwordswithall($dict_arr, $rack_arr));?>
</pre>
<?php


function findwordswithany($dict_arr, $rack_arr){
            $working_arr = $rack_arr;
    $found_arr = array();
    foreach($dict_arr as $word){    
        foreach($working_arr as $key => $letter){
            if(strpos($word, $letter) !== false){               
                $found_arr[] = $word;
               //Modified to remove the letter from further 
               unset($working_arr[$key]);
                break 1;
            }
        }
                    $working_arr = $rack_arr;   
    }
    if(count($found_arr) > 0)
        return $found_arr;
    else
        return false;
}

function findwordswithall($dict_arr, $rack_arr){
    $found_arr = array();
    foreach($dict_arr as $word){    
        $foundall = true;
        foreach($rack_arr as $letter){
            if(strpos($word, $letter) === false){               
                $foundall = false;
                break 1;
            }
        }
        if($foundall === true){
        $found_arr[] = $word;
        }
    }
    if(count($found_arr) > 0)
        return $found_arr;
    else
        return false;
}
?>