user3096443
user3096443

Reputation: 180

How can I check two strings for similarity in php?

Say I have two strings

$string1 = "Hello my name is Steve and this is spam";
$string2 = "Hello my name is Steven and this comment is spam";

These two strings are alike.

Is there a way to compare these, as in if ($string1 like $string2) in php?

Upvotes: 2

Views: 1033

Answers (2)

Powerriegel
Powerriegel

Reputation: 627

Then hash the strings and compare the hash. I think thats faster than any string compare function.

Reason. String compare functions compare every character - byte by byte. A hash function takes the whole bunch of chars at once and generates its hash.

But of course you should use an "easy" hash function like md5. Not hash_pbkdf which is specialised for secure hashes. That means pbkdf computes the hash very slow so an atacker can't generate so many hashes at a given time. But thats off topic ;-)

Or: Implement this https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm or even better (faster) https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string_search_algorithm

Upvotes: -5

jwueller
jwueller

Reputation: 30996

You can calculate a similarity index using the levenshtein algorithm. It calculates the number of characters that need to be changed in order to transform the first input into the second, or the other way around:

levenshtein("bar", "baz"); // 1 character difference
levenshtein("bar", "foo"); // 3 character difference

$string1 = "Hello my name is Steve and this is spam";
$string2 = "Hello my name is Steven and this comment is spam";
levenshtein($string1, $string2); // 9 character difference

@TimCooper also suggested similar_text(), which works in a similar fashion.

Upvotes: 6

Related Questions