Forum Moderators: coopster

Message Too Old, No Replies

Similar values

comparrison question

         

WhosAWhata

2:03 am on Sep 4, 2005 (gmt 0)

10+ Year Member



is there any way for php to "compare" values in an array to a string and try to find which is most "similar"

$string = "aple";
$array = array("apple","orange","monkey");
$similar = coolfunction($string,$array);
echo $similar; // apple

is there a way to accomplish this?

ergophobe

3:25 pm on Sep 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Depending on what you mean by most similar (looks like or sounds like) these functions can help you out:

similar_text();
soundex();
metaphone();
levenshtein();

Since levenshtein is O(m*n) and to similar_text() is O(max(n,m)**3), I would go with levenshtein if it works for you.

PS O (aka "Big O" in computer speak) is how an operation scales in terms of actions (rather than CPU time or memory or some physical measure). In other words, it's a mathematical measure of efficiency. So levenshtein takes m*n operations whereas similar_text takes the longest string, m or n, and then you cube it, so it scales very poorly to long strings.