php - Finding nearest percentage match to 2 strings, and returning next closest -


i have code takes user inputted value , column mysql table, need run percentage comparison , return percentage, highest first, second highest, third highest , on.....

this code using far:

<?php    $result_while = mysql_query("select name store_table");    $array = array();  while ($row = mysql_fetch_array($result_while)) {    $array[] = $row[name];  }    $words  = $array;    $string2= "mr peter smith";    foreach ($words $word) {    $percent = (1 - levenshtein($word, $string2)/max( strlen($word),strlen($string2) ) ) * 100;    $word . " - percent match is: " . $percent[$word] . " </br>";  //60%    }      function comparestrings($s1, $s2) {      //one empty, no result      if (strlen($s1)==0 || strlen($s2)==0) {          return 0;      }        //replace none alphanumeric charactors      //i left - in case used combine words      $s1clean = preg_replace("/[^a-za-z0-9-]/", ' ', $s1);      $s2clean = preg_replace("/[^a-za-z0-9-]/", ' ', $s2);        //remove double spaces      while (strpos($s1clean, "  ")!==false) {          $s1clean = str_replace("  ", " ", $s1clean);      }      while (strpos($s2clean, "  ")!==false) {          $s2clean = str_replace("  ", " ", $s2clean);      }        //create arrays      $ar1 = explode(" ",$s1clean);      $ar2 = explode(" ",$s2clean);      $l1 = count($ar1);      $l2 = count($ar2);        //flip arrays if needed ar1 largest.      if ($l2>$l1) {          $t = $ar2;          $ar2 = $ar1;          $ar1 = $t;      }        //flip array 2, make words keys      $ar2 = array_flip($ar2);          $maxwords = max($l1, $l2);      $matches = 0;        //find matching words      foreach($ar1 $word) {          if (array_key_exists($word, $ar2))              $matches++;      }        return ($matches / $maxwords) * 100;      }  ?>

this works fine , gives me long list of names , percentage value next them. need highest first, , in order.....

i need output range select dropdown box if makes sense..... have ability pick highest out display in separate row on page.....

thanks in advance.

damian

try making line:

foreach ($words $word) {     $percent = (1 - levenshtein($word, $string2)/max(strlen($word),strlen($string2) ) ) * 100;     $word . " - percent match is: " . $percent[$word] . " </br>"; //60% } 

this:

foreach ($words $word) {     $percent[$word] = (1 - levenshtein($word, $string2)/max( strlen($word),strlen($string2) ) ) * 100; } 

then sort array largest smallest:

$percent = arsort($percent); 

then echo this:

foreach ($percent $key => $value) {    echo $key . " - percent match is: " . $value . " </br>"; } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -