A blog & community for non-professional web designers, programmers, webmasters, and other web savvies.

Text Similarity

February 28, 2011 - 12:00am -- Thomas

This script calculates how similar two strings of text are.

<?php
/*****************
--Text Similarity
--More PHP Scripts at
--http://icecaves.net/tags/php
*****************/
$string_one = "This script will calculate how many characters are similar in two strings. It also returns a percent of how much the strings are similar.";
$string_two = "Below you can see how many characters match in each string.";
$calculate = similar_text($string_one, $string_two, $percent);
$percent = round($percent, 2);
echo
"<b>String One:</b> $string_one<br><b>String Two:</b> $string_two<br><b>Matching Characters:</b> $calculate<br><b>Percent:</b> $percent%\n";
?>
Code Preview: 
String One: This script will calculate how many characters are similar in two strings. It also returns a percent of how much the strings are similar.
String Two: Below you can see how many characters match in each string.
Matching Characters: 39
Percent: 39.8%
Tags: 
Forum Board: 

Comments

darkdc
darkdc's picture
Snowball (?)
Rank Points: 8 (?)
Posts: 9
Status: Offline

Other functions you an use to kinda find similarity are those that deal with pronunciations and special "rules".

Some other functions:
Metaphone
SoundEx
Levenshtein

These are great if you wanted to provide features like spelling suggestions or an advanced search.

@IceCaves on Twitter

    Stay Connected

    Twitter Facebook Tumblr Google+ Glaciallis Google RSS

    *Follow us on Twitter or Facebook to also follow minor site updates that don't make it to the homepage news.