atrsimilar_similarity

Versions
6.1
atrsimilar_similarity($string_a, $string_b)

Find two strings their similarity.

Parameters

$string_a The first string.

$string_b The second string.

Return value

The strings' similarity in percents.

Code

atr/modules/atrsimilar/includes/atrsimilar.review.inc, line 93

<?php
function atrsimilar_similarity($string_a, $string_b) {
  $diff = atrsimilar_diff($string_a, $string_b);
  $length = array(drupal_strlen($string_a), drupal_strlen($string_b));
  $length_common = 0;
  foreach ($diff[0] as $token) {
    if ($token['common']) {
      $length_common += drupal_strlen($token['string']);
    }
  }

  // We define the total amount of characters as the total length of the common
  // subsequences and all characters that are different.
  $total = $length[0] + $length[1] - $length_common;

  return $similarity = $length_common / $total * 100;
}
?>