简体   繁体   English

PHP相似词使用soundex和数组

[英]PHP Similar Words using soundex with array

I am creating a findSpellings function that has two parameters $word and $allWords. 我正在创建一个具有两个参数$ word和$ allWords的findSpellings函数。 $allwords is an array that has mis-spellings of words that could sound similar to the $word variable. $ allwords是一个数组,其中包含拼写错误的单词,听起来与$ word变量相似。 What I am trying to accomplish is to print out all words that are similar to the $word based on the soundex function. 我要完成的工作是基于soundex函数打印出与$ word相似的所有单词。 I am having trouble printing out the array with words. 我在用单词打印数组时遇到麻烦。 My function that I have is below. 我的功能如下。 Any help would be greatly appreciated: 任何帮助将不胜感激:

<?php 
$word = 'stupid';

$allwords = array(
    'stupid',
    'stu and pid',
    'hello',
    'foobar',
    'stpid',
    'supid',
    'stuuupid',
    'sstuuupiiid',
);
function findSpellings($word, $allWords){
while(list($id, $str) = each($allwords)){

    $soundex_code = soundex($str);

    if (soundex($word) == $soundex_code){
        //print '"' . $word . '" sounds like ' . $str;
        return $word;
        return $allwords;
    }
    else {
        return false;
    }

}
 }
 print_r(findSpellings($word, $allWords));
?>
if (soundex($word) == $soundex_code){
    //print '"' . $word . '" sounds like ' . $str;
    return $word;
    return $allwords;
}

You can't have 2 returns, the first return will exit the code. 您不能有2个返回,第一个返回将退出代码。

You could just do something like this: 您可以这样做:

if (soundex($word) == $soundex_code){
    //print '"' . $word . '" sounds like ' . $str;
    $array = array('word' => $word, 'allWords' => $allWords);
    return $array;
}

And then just retrieve the values out of $array like so: 然后像这样从$ array中检索值:

$filledArray = findSpellings($word, $allWords);

echo "You typed".$filledArray['word'][0]."<br/>";
echo "Were you looking for one of the following words?<br/>";

foreach($filledArray['allWords'] as $value)
{
    echo $value;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM