similar_text

(PHP 4, PHP 5, PHP 7)

similar_text — Calculate the similarity between two strings

Descrierea

intsimilar_text
( string$first
, string$second
[, float&$percent
] )

This calculates the similarity between two strings as described in
Programming Classics: Implementing the World's Best Algorithms by Oliver (ISBN 0-131-00413-1). Note that this implementation does not use a
stack as in Oliver's pseudo code, but recursive calls which may or may not
speed up the whole process. Note also that the complexity of this algorithm
is O(N**3) where N is the length of the longest string.

Parametri

first

The first string.

second

The second string.

Notă:

Swapping the first and
second may yield a different result; see the
example below.

percent

By passing a reference as third argument,
similar_text() will calculate the similarity in
percent, by dividing the result of similar_text() by
the average of the lengths of the given strings times
100.

Valorile întoarse

Returns the number of matching chars in both strings.

The number of matching characters is calculated by finding the longest first
common substring, and then doing this for the prefixes and the suffixes,
recursively. The lengths of all found common substrings are added.

Exemple

Example #1 similar_text() argument swapping example

This example shows that swapping the first and
second argument may yield different results.

Be aware when using this function, that the order of passing the strings is very important if you want to calculate the percentage of similarity, in fact, altering the variables will give a very different result, example :

Recursive algorithm usually is very elegant one. I found a way to get better precision without the recursion. Imagine two different (or same) length ribbons with letters on each. You simply shifting one ribbon to left till it matches the letter the first.

The speed issues for similar_text seem to be only an issue for long sections of text (>20000 chars).

I found a huge performance improvement in my application by just testing if the string to be tested was less than 20000 chars before calling similar_text.

20000+ took 3-5 secs to process, anything else (10000 and below) took a fraction of a second.Fortunately for me, there was only a handful of instances with >20000 chars which I couldn't get a comparison % for.

If you have reserved names in a database that you don't want others to use, i find this to work pretty good.
I added strtoupper to the variables to validate typing only. Taking case into consideration will decrease similarity.

Well, as mentioned above the speed is O(N^3), i've done a longest common subsequence way that is O(m.n) where m and n are the length of str1 and str2, the result is a percentage and it seems to be exactly the same as similar_text percentage but with better performance... here's the 3 functions i'm using..

you can skip calling str_lcsfix if you don't worry about accentuated characters and things like that or you can add up to it or modify it for faster performance, i think ereg is not the fastest way?
hope this helps.
Georges

@I_HATE_SPAMMER- PAZ! (http://php.net/similar_text#115994)Not only will your code fail the "turkey test" (google it), and for a matter most comments here that use strtoupper()/strtolower(), it also depends very much on the use-case; maybe I wanted the 75% ? I don't see the value of such comments; this is something everybody could've come op with. It's basic programming and turning requirements into algorithms.