idalatob
09-17-2007, 10:34 AM
Hi guys I thought I would add a small function that I use for matching up strings . It is pretty useful after performing a search through a database to order it by relavancy. It uses a combination of similar_text() and levenshtein to make a powerful search tool.
THE FUNCTION:
<?php
/* $old_w = The array of words you want to match up to.
$new_w = The word you want to check.
*/
$result_array = array(); //creates an empty array
function getSimilar($new_w,$old_w){
global $result_array; //Make a global, because it (the variable,$result_array) is only defined in the function.
foreach($old_w as $part){ //loop through the array of words.
$sim = similar_text($new_w,$part); //run similar text on each word in the array.
$lev = levenshtein($new_w,$part); //run levenshtein on each word in the array.
if ($sim != 0){ //This part doesnt execute if sim = 0, as this means the word is not a match at all (no character matches).
$lev_error = intval(($lev / (strlen($part)) * 100)); //calculate the levenshtein percentage (error percentage)
$per_sim = intval($sim / (strlen($new_w))) * 100; //calculate the similar text percentage
$total = $per_sim - $lev_error; //work out the total percentage
} else{ //if sim = 1 then the total % match == 0
$total = 0;
}
$result_array[$part] = $total; // add the total & word to the array.
}
arsort($result_array); //Sort the array with the highest match first
return $result_array; //return the value
} ?>
CALLING THE FUNCTION:
<?php
$words = explode(",","lasse,manson,name,time,lassie,svend,svendie,jordan,grommit,plethora,tristan,noka,mark,ib,britte");
/* The above is an array of words that I want to match "lasse" up to.
A good use for this list of words is to search through a database
and slap this result into this array */
$name = getSimilar("lasse",$words); //call the array
print_r($result_array); //print the result
?>
THE FUNCTION:
<?php
/* $old_w = The array of words you want to match up to.
$new_w = The word you want to check.
*/
$result_array = array(); //creates an empty array
function getSimilar($new_w,$old_w){
global $result_array; //Make a global, because it (the variable,$result_array) is only defined in the function.
foreach($old_w as $part){ //loop through the array of words.
$sim = similar_text($new_w,$part); //run similar text on each word in the array.
$lev = levenshtein($new_w,$part); //run levenshtein on each word in the array.
if ($sim != 0){ //This part doesnt execute if sim = 0, as this means the word is not a match at all (no character matches).
$lev_error = intval(($lev / (strlen($part)) * 100)); //calculate the levenshtein percentage (error percentage)
$per_sim = intval($sim / (strlen($new_w))) * 100; //calculate the similar text percentage
$total = $per_sim - $lev_error; //work out the total percentage
} else{ //if sim = 1 then the total % match == 0
$total = 0;
}
$result_array[$part] = $total; // add the total & word to the array.
}
arsort($result_array); //Sort the array with the highest match first
return $result_array; //return the value
} ?>
CALLING THE FUNCTION:
<?php
$words = explode(",","lasse,manson,name,time,lassie,svend,svendie,jordan,grommit,plethora,tristan,noka,mark,ib,britte");
/* The above is an array of words that I want to match "lasse" up to.
A good use for this list of words is to search through a database
and slap this result into this array */
$name = getSimilar("lasse",$words); //call the array
print_r($result_array); //print the result
?>