Similar text

Shows how to use the similar_text() function to compare similar words.
It returns how similar the words are.

Snippet information

Author:
Jonas John

License:
Public Domain

Language:
PHP

Created:
06/28/2006

Updated:
06/28/2006

Tags:
,


$word2compare = "stupid";
 
$words = array(
    'stupid',
    'stu and pid',
    'hello',
    'foobar',
    'stpid',
    'upid',
    'stuuupid',
    'sstuuupiiid',
);
 
while(list($id, $str) = each($words)){
    similar_text($str, $word2compare, $percent);
    print "Comparing '$word2compare' with '$str': ";
    print round($percent) . "%\n";
}
 
/*
Results:
 
Comparing 'stupid' with 'stupid': 100%
Comparing 'stupid' with 'stu and pid': 71%
Comparing 'stupid' with 'hello': 0%
Comparing 'stupid' with 'foobar': 0%
Comparing 'stupid' with 'stpid': 91%
Comparing 'stupid' with 'upid': 80%
Comparing 'stupid' with 'stuuupid': 86%
Comparing 'stupid' with 'sstuuupiiid': 71%
*/


Found a bug? Or do you have a better solution for this?
Feel free to leave a message:

Add a comment


Leave a comment

rem45acp June 15, 2009 at 06:08
what if you wanted to find the simularity between all of those words? instead of comparing 'stupid' to all the others, what if you wanted to find which one out of all of the words is the most similar to the others?
Per Nielsen April 03, 2008 at 01:20
Not bad, nice one!