あなたは誰かが使用法についてアイデアを持っているかどうかを尋ねました.まあ、これはPHP
サイトの例ですが、それがあなたの助けになると思います.
(あなたのサイトのエクスペリエンスの 1 つに適合するようにコードを変更しました):
<?php
$productString= 'Apple white IPOD';
// array of words to check against
$products = array('zen','dell laptop','apple laptop','apple black ipod',
'apple mini','Random product');
// no shortest distance found, yet
$shortest = -1;
// loop through products to find the closest product
foreach ($products as $product) {
// calculate the distance between the input word,
// and the current word
$lev = levenshtein($productString, $product);
// check for an exact match
if ($lev == 0) {
// closest word is this one (exact match)
$closest = $product;
$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 "Search product: $productString\n";
if ($shortest == 0) {
echo "Exact match found: $closest\n";
} else {
echo "Did you mean: $closest?\n";
}
?>
上記のコードは、製品のリスト、配列を検索し、最も近い一致を見つけます。完全一致が見つかった場合は、代わりにそれが使用されます。