我想对mysql查询结果执行levenshtein。PHP Levenshtein查询结果
查询看起来是这样的:
$query_GID = "select `ID`,`game` from `gkn_catalog`";
$result_GID = $dbc->query($query_GID);
$row_GID = mysqli_fetch_array($result_GID,MYSQLI_ASSOC);
在这里,我准备在莱文斯坦操作一切:
$shortest = -1;
$input = $game_title;
而这仅仅是从手动的莱文斯坦操作:
foreach ($row_GID as $row) {
$word = $row['game'];
// calculate the distance between the input word,
// and the current word
$lev = levenshtein($input, $word);
// check for an exact match
if ($lev == 0) {
// closest word is this one (exact match)
$closest = $word;
$shortest = 0;
// break out of the loop; we've found an exact match
break;
}
// if this distance is less than the next found shortest
// distance, OR if a next shortest word has not yet been found
if ($lev <= $shortest || $shortest < 0) {
// set the closest match, and shortest distance
$closest = $word;
$shortest = $lev;
}
}
echo "Input word: $input\n";
if ($shortest == 0) {
echo "Exact match found: $closest\n";
} else {
echo "Did you mean: $closest?\n";
}
感谢Jaitsu我摆脱了错误/警告消息,但levenshtein现在扔荷兰国际集团一个意想不到的结果:
无论输入的是,它永远不会找到匹配的结果,并且最接近的匹配将始终= ^h
例子:
输入字:战地3回到Karkand你的意思是:H?
输入单词: 星际争霸2自由之翼你的意思是:H?
说实话,我没有什么线索#回事现在...
是什么'$ words'?这不是一个数组,这就是你的问题 – JamesHalsall 2013-03-11 12:52:33
那么我如何使它成为一个数组呢?我想要它存储我的查询结果...手册声明一个数组是这样的:'$ words = array('apple','pineapple','banana','orange', '萝卜','胡萝卜','豌豆','豆','马铃薯');'但是我怎么用我的查询结果声明一个数组? – SubZero 2013-03-11 12:55:11
请参阅下面的答案 – JamesHalsall 2013-03-11 12:58:14