The similar_text Function

The similar_text function takes two strings for comparison and returns the number of matching characters. The percentage of similarity can be obtained as an optional third parameter. The comparison is case-sensitive.

Syntax

phpsimilar_text(string1, string2, [&percent]);

Example

Let's compare two similar strings and get the number of matching characters:

php<?php
  $res = similar_text('Hello', 'Hallo');
  echo $res;
?>

Code execution result:

php4

Example

Get the percentage of string matching:

php<?php
  similar_text('Hello', 'Hallo', $percent);
  echo $percent;
?>

Code execution result:

php80

Example

Comparing strings with different case:

php<?php
  similar_text('Hello', 'hello', $percent);
  echo $percent;
?>

Code execution result:

php80

See Also

  • the levenshtein function,
    which calculates the Levenshtein distance between strings
  • the soundex function,
    which calculates the soundex key of a string