我正在尝试完成一个小型的个人项目。我需要取一串字符并尝试从所述字符串的变体中“创建”单词;检查带有已知单词列表的文本文件(单词用新行分隔)。
总结:
我脑子里有这个概念,不知道应该怎么做,我只是想找一个可以向我解释过程的人。
<?php
// list of words, one per line
$dictionary = file_get_contents('dictionary.txt');
// provided characters, in the end by user
$chars_provided = "a,t,w,q,u,i,f,d,s,b,v,x,o";
// count the total # of characters
$chars_count = strlen($chars_provided);
// display given information so far
echo "The letters '$chars_provided' were entered, totaling $chars_count letters.";
// explode the characters by using the comma designator
$break_chars = explode(",", $chars_provided);
foreach ($break_chars as $letter) {
echo "$letter[0]";
}
答案 0 :(得分:2)
如果您获得字典中每个单词的字母数,保留它,然后匹配用户输入字符数,这会更容易。
例如,对于'aaab',任何小于(或等于)3'a,小于(或等于)1'b的单词,并且没有其他字符匹配。
//// 1. Grab letter counts for your user input.
$user_input_chars = 'abcdefg'; // for example
$user_in_letter_counts = get_letter_counts($user_input_chars);
// $letters[$char][$num] will contain all words that have exactly $num number of $char characters
$letters = array('a' => array(), 'b' => array(), /* ...,*/ 'z' => array());
//// 2. Generate list of words with at least $number_of quantity of $letter characters
// (only have to be done once for any amount of user input if you keep this in memory)
foreach ($words as $word){
// get letter counts for each type of character for this word
$letter_counts = get_letter_counts($word);
// store in array of letters and count
foreach($letter_counts as $letter => $number_of){
// we already have a word that had $number_of $letter characters; add word to existing array
if (isset($letters[$letter][$number_of])){
$letters[$letter][$number_of][] = $word;
} // make array to record that this word has $number_of $letter characters
else {
$letters[$letter][$number_of] = array($word);
}
$number_of--;
}
}
//// 3. Find matching words.
$potential_words = array();
foreach ($letters as $letter => $arr){
foreach($arr as $num => $words){
// if this array has less than or equal to the number of $letter characters that the user input has,
// add the words to the potential match list for that character
if ($num <= $arr[$user_in_letter_counts[$letter]]){
$potential_words[$letter] = array_merge($potential_words[$letter], $words);
}
}
}
// the words must have met the requirement for each character, so only grab words that satisfy all conditions
$all_matching_words = array_intersect($potential_words['a'], $potential_words['b'], /* ..., */ $potential_words['z']);
// (It should be trivial to just grab 100 of these.)
function get_letter_counts($word){
$result = array();
$result['a'] = substr_count($my_word, 'a');
$result['b'] = substr_count($my_word, 'b');
// ...
$result['z'] = substr_count($my_word, 'z');
return $result;
}
答案 1 :(得分:0)
希望你能用它。
$file = file_get_contents("dictionary.txt");
$SearchString = "jdlwhfushfmgh/maybeasencondword";
$breakstrings = explode('/',$SearchString);
foreach ($breakstrings as $values)
{
if(!strpos($file, $values))
{
echo $values." string not found!\n";
}
else
{
echo $values." string Found!\n";
}