我想對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