note 50699 added to function.levenshtein

From: Date: Mon, 07 Mar 2005 17:01:48 +0000
Subject: note 50699 added to function.levenshtein
Groups: php.notes 
Request: Send a blank email to php-notes+get-86058@lists.php.net to get a copy of this message
I am using the Levenshtein distance to SORT my search results. I have a search page for peoples names. I do a SOUNDEX() search on the name in mysql. MySQL SOUNDEX() will perform the "fuzzy" search for me. I then calculate the Levenshtein distance between the search term and the actual name found by the SOUNDEX() search. This will give me a score on how close my results are to the search string. I can the sort my results for display listing the closest results first. <?php // PHP CODE INCLUDING DB LOOKUPS HERE usort($searchresults, "finallevenshteinsortfunction"); function finallevenshteinsortfunction($a, $b) { if(($a['levenshtein'] > $b['levenshtein']) || ( $a['levenshtein'] == $b['levenshtein'] && strnatcasecmp( $a['Last_Name'], $b['Last_Name']) >= 1) ){ return $a['levenshtein'];} // Ok... The levenstein is greater OR with the same levenshtein, the last name is alphanumerically first elseif($a['levenshtein'] == $b['levenshtein']){ return '0';} // The levenstein matches elseif($a['levenshtein'] < $b['levenshtein']){ return -$a['levenshtein'];} else{die("<!-- a horrable death -->");} } ?> ---- Manual Page -- http://www.php.net/manual/en/function.levenshtein.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+50699 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50699&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50699&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50699&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50699&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50699&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+50699&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#86058) next »