-1

In PHP there are several functions or methods to compare the equality between two strings. For example "Hello world" == "Hello world". But let's say that I have strings that are almost equal. Can these be compared and like get a value of similarity (i.e. 97% are equal).

<?php
$a = "Stack Overflow";
$b = "Stack-Overflow";
$result = str_compare_similarities($a, $b); // should return something like 0.958 for example
?>

I don't think that there's a function for this in PHP (or is there?). MySQL has for instance the function FULLTEXT that compares how much two strings are equal. But can it be solved with PHP?

Gowire
  • 1,046
  • 6
  • 27

1 Answers1

1

similar_text — Calculate the similarity between two strings

Description

similar_text(string $string1, string $string2, float &$percent = null): int

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.

For more info click here

Ajeet
  • 286
  • 2
  • 5