note 40310 added to function.levenshtein

From: Date: Sat, 28 Feb 2004 07:52:25 +0000
Subject: note 40310 added to function.levenshtein
Groups: php.notes 
Request: Send a blank email to php-notes+get-65921@lists.php.net to get a copy of this message
Some code for a basic fuzzy search. <?php $needle = "Youser Nein stitched tymes hand"; $haystack = " Did you know that a stitch in time can save nine. Isn't that amazing! I think so The user-supplied function has to return the loser, from the sand. Describe the cost for this particular operation. Eweser may decide to use only some of the supplied nans. And pay attention. This isn't hard... "; // explode into words $hwords = preg_split("/[\s\W]+/", $haystack); $nwords = preg_split("/[\s\W]+/", $needle); echo "You searched for $needle<br>"; echo "I found...<br>"; foreach ($hwords as $hkey => $hayword) { $hmp = metaphone ($hayword); foreach ($nwords as $nkey => $needword) { // First or last letters of needle and haystack have to match (case insensitive) $nfirst = strtolower(substr($needword, 0, 1)); $nlast = strtolower(substr($needword, -1)); $hfirst = strtolower(substr($hayword, 0, 1)); $hlast = strtolower(substr($hayword, -1)); if (($hfirst == $nfirst) or ($hlast == $nlast)) { $nmp = metaphone ($needword); $distance = levenshtein ($hmp, $nmp); // $distance = levenshtein ($hayword, $needword); $n_len = strlen($nmp); $per = round(($distance/$n_len)*1000); if ($per < 335) { // Highlight word in haystack $haystack = str_replace($hayword, "<b>$hayword</b>", $haystack); $haystack = str_replace("<b><b>", "<b>", $haystack); $haystack = str_replace("</b></b>", "</b>", $haystack); } } } } // echo the new haystack echo $haystack; // Returns ... // // You searched for Youser Nein stitched tymes hand // I found... // Did you know that a <b>stitch</b> in <b>time</b> can // save <b>nine</b>. Isn't that amazing! I think so. The // <b>user</b>-supplied function has to return the // <b>loser</b>, from the sand. Describe the cost for this // particular operation. Eweser may decide to use only some // of the supplied nans. <b>And</b> pay attention. This // isn't <b>hard</b>... ?> ---- Manual Page -- http://www.php.net/manual/en/function.levenshtein.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+40310 Delete -- http://master.php.net/manage/user-notes.php?action=delete+40310&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+40310&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#65921) next »