Quantcast
Channel: PHP Levenshtein on Query Result - Stack Overflow
Viewing all articles
Browse latest Browse all 2

PHP Levenshtein on Query Result

$
0
0

I want to perform a levenshtein on a mysql query result.

The query looks like this:

$query_GID = "select `ID`,`game` from `gkn_catalog`";$result_GID = $dbc->query($query_GID);$row_GID = mysqli_fetch_array($result_GID,MYSQLI_ASSOC);

And here I prepare everything for the levenshtein operation:

$shortest = -1;$input = $game_title;

And this is just the levenshtein operation from the manual:

   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 matchif ($lev == 0) {// closest word is this one (exact match)$closest = $word;$shortest = 0;// break out of the loop; we've found an exact matchbreak;}// if this distance is less than the next found shortest// distance, OR if a next shortest word has not yet been foundif ($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";}

Thanks to Jaitsu I got rid of the error/warning message, however the levenshtein is now throwing an unexpected result:

Whatever the input is, it won't ever find a matching result and the closest match will always be = H

Examples:

Input word: Battlefield 3 Back to Karkand Did you mean: H?
Input word: Starcraft 2 Wings of Liberty Did you mean: H?

To be honest, I don't have a clue what#s going on right now...


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images